如何将 NSRange 与整数一起使用来简化我的代码?
我刚刚开始学习 Objective-C 并制作了一个小指南针应用程序,当它落入一系列标题时,它会显示方向。它工作得很好,但我想知道是否有更简洁的方法使用 NSRange 来编写它。经过大量查找后,似乎 NSRange
更多地用于字符串函数而不是数字。
我尝试以 NSRange
的实例作为起点,以使其更加简洁,但我无法找到可以查找数字是否落在 NSRange
内的函数。
我是否走在正确的轨道上,或者我是否让这个变得比需要的更冗长?
预先感谢..
这是我尝试缩短代码失败的起点:
// If heading falls within this range, then display "S" for south
NSRange eastenRange = NSMakeRange (80, 100);
NSRange southernRange = NSMakeRange (170, 190);
etc...
这是我当前的代码(工作正常):
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
// Define and display the heading
NSNumber *theHeading = [NSNumber numberWithInt:[newHeading trueHeading]];
[headingLabel setText:[NSString stringWithFormat:@"%@°", theHeading]];
// Define the range of directions
NSNumber *northLowerRange = [NSNumber numberWithInt:10];
NSNumber *northUpperRange = [NSNumber numberWithInt:350];
NSNumber *eastLowerRange = [NSNumber numberWithInt:80];
NSNumber *eastUpperRange = [NSNumber numberWithInt:100];
NSNumber *southLowerRange = [NSNumber numberWithInt:170];
NSNumber *southUpperRange = [NSNumber numberWithInt:190];
NSNumber *westLowerRange = [NSNumber numberWithInt:260];
NSNumber *westUpperRange = [NSNumber numberWithInt:280];
// If the heading falls within the correct ranges, then display the direction
if ([northLowerRange compare:theHeading] == NSOrderedDescending || [northUpperRange compare:theHeading] == NSOrderedAscending)
[directionLabel setText:@"N"];
else if ([eastLowerRange compare:theHeading] == NSOrderedAscending && [eastUpperRange compare:theHeading] == NSOrderedDescending)
[directionLabel setText:@"E"];
else if ([southLowerRange compare:theHeading] == NSOrderedAscending && [southUpperRange compare:theHeading] == NSOrderedDescending)
[directionLabel setText:@"S"];
else if ([westLowerRange compare:theHeading] == NSOrderedAscending && [westUpperRange compare:theHeading] == NSOrderedDescending)
[directionLabel setText:@"W"];
else
[directionLabel setText:@"-"];
}
I've just started learning Objective-C and made a little compass app that will display a direction when it falls into a range of headings. It works just fine, but I wonder if there is a more concise way of writing it using NSRange
. After a lot of looking, it seems like NSRange
is used more for string functions rather than numbers.
I tried to make an instance of NSRange
my starting point to make this more concise, I couldn't track down the function that would find if a number falls within an NSRange
.
Am I on the right track here, or am I making this more verbose than it needs to be?
Thanks in advance..
Here was my failed jumping off point for attempting to shorten up the code:
// If heading falls within this range, then display "S" for south
NSRange eastenRange = NSMakeRange (80, 100);
NSRange southernRange = NSMakeRange (170, 190);
etc...
Here is my current code (works fine):
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
// Define and display the heading
NSNumber *theHeading = [NSNumber numberWithInt:[newHeading trueHeading]];
[headingLabel setText:[NSString stringWithFormat:@"%@°", theHeading]];
// Define the range of directions
NSNumber *northLowerRange = [NSNumber numberWithInt:10];
NSNumber *northUpperRange = [NSNumber numberWithInt:350];
NSNumber *eastLowerRange = [NSNumber numberWithInt:80];
NSNumber *eastUpperRange = [NSNumber numberWithInt:100];
NSNumber *southLowerRange = [NSNumber numberWithInt:170];
NSNumber *southUpperRange = [NSNumber numberWithInt:190];
NSNumber *westLowerRange = [NSNumber numberWithInt:260];
NSNumber *westUpperRange = [NSNumber numberWithInt:280];
// If the heading falls within the correct ranges, then display the direction
if ([northLowerRange compare:theHeading] == NSOrderedDescending || [northUpperRange compare:theHeading] == NSOrderedAscending)
[directionLabel setText:@"N"];
else if ([eastLowerRange compare:theHeading] == NSOrderedAscending && [eastUpperRange compare:theHeading] == NSOrderedDescending)
[directionLabel setText:@"E"];
else if ([southLowerRange compare:theHeading] == NSOrderedAscending && [southUpperRange compare:theHeading] == NSOrderedDescending)
[directionLabel setText:@"S"];
else if ([westLowerRange compare:theHeading] == NSOrderedAscending && [westUpperRange compare:theHeading] == NSOrderedDescending)
[directionLabel setText:@"W"];
else
[directionLabel setText:@"-"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(4)
我是否让它变得比需要的更冗长?
是的。当你想要进行数值运算时,请避免使用 NSNumber。 NSNumber 类的存在只是因为像 NSArray、NSDictionary 等 Objective-C 集合只能保存 Objective-C 对象。否则,您应该始终使用普通的 int
或 NSInteger
或 CGFloat
或 double
等。
int heading = [newHeading trueHeading];
headingLabel.text = [NSString stringWithFormat:@"%d°", heading];
if (10 < heading || heading > 350)
directionLabel.text = @"N";
else if (80 < heading && heading < 100)
directionLabel.text = @"E";
// and so on.
您不需要使用 NSRange。
如果您想要更集成地使用 NSRange 结构,我发现它对于比较数组的部分很有用:
NSRange aRange = NSRangeFromString([NSString stringWithFormat:@"{0:%d}",items.count]);
NSIndexSet* aSet = [NSIndexSet indexSetWithIndexesInRange:aRange];
NSIndexSet *hasNotBeenReadSet = [items indexesOfObjectsAtIndexes:aSet
options:NSEnumerationConcurrent
passingTest:
^BOOL(BasicItem* obj, NSUInteger idx, BOOL *stop) {
return [obj hasNotBeenRead];
}];
int numberOfUnreadItems = hasNotBeenReadSet.count;
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
参加聚会迟到了,但以下方法可以工作并利用我相信的范围:
等等。
Coming late to the party but the following would work and make use of ranges I believe:
etc. etc.