您可以通过使用命名的常量,而不是幻数
typedef NS_ENUM(NSInteger, Heading) {
HeadingNorth = -1,
HeadingNorthEast,
HeadingEast,
HeadingSouthEast,
HeadingSouth,
HeadingSouthWest,
HeadingWest,
HeadingNorthWest
};
增加可读性你可以比改变你的switch语句
Heading heading = floor((trueHeadingRounded - 23)/45);
NSString *cardinalDirection;
switch (heading) {
case HeadingNorth: cardinalDirection = @"north"; break;
case HeadingNorthEast: cardinalDirection = @"northeast"; break;
case HeadingEast: cardinalDirection = @"east"; break;
case HeadingSouthEast: cardinalDirection = @"southeast"; break;
case HeadingSouth: cardinalDirection = @"south"; break;
case HeadingSouthWest: cardinalDirection = @"southwest"; break;
case HeadingWest: cardinalDirection = @"west"; break;
case HeadingNorthWest: cardinalDirection = @"northwest"; break;
}
NSLog(@"%@", cardinalDirection);
好点。我评论了我的所有代码,所以可理解性不是问题(我希望)。我只是不喜欢在我的项目中有大约30行代码,最多可以缩短到4行。 –