Ios 如何使用带整数的NSRange简化代码?

Ios 如何使用带整数的NSRange简化代码?,ios,objective-c,nsrange,Ios,Objective C,Nsrange,我刚刚开始学习Objective-C,并制作了一个小的compass应用程序,当它落在一系列标题中时,它会显示一个方向。它工作得很好,但我想知道是否有一种更简洁的方法可以使用NSRange编写它。经过大量查看,似乎NSRange更多地用于字符串函数,而不是数字 我试图将NSRange的一个实例作为我的出发点,以使其更加简洁,我无法找到一个函数,该函数将查找某个数字是否在NSRange中 我是在正确的轨道上,还是说我把事情说得比实际需要的更详细 提前谢谢 以下是我试图缩短代码的失败起点: // I

我刚刚开始学习Objective-C,并制作了一个小的compass应用程序,当它落在一系列标题中时,它会显示一个方向。它工作得很好,但我想知道是否有一种更简洁的方法可以使用
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...
以下是我当前的代码(工作正常):


范围具有位置和长度。因此,如果您想要东部范围为80到100度,可以使用NSMakeRange(80,20)。这将创建一个范围,从80度开始,跨越20度

我是不是把它说得比实际需要的更详细了

对。当您想进行数值运算时,请避免使用NSNumber。NSNumber类的存在只是因为Objective-C集合(如NSArray、NSDictionary等)只能保存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.
int heading=[newHeading-trueHeading];
headingLabel.text=[NSString stringWithFormat:@“%d°,heading];
如果(10<标题| |标题>350)
directionLabel.text=@“N”;
否则如果(80<标题和标题<100)
directionLabel.text=@“E”;
//等等。

您不需要使用NSRange。

在聚会上迟到,但以下几点可以发挥作用,我相信可以利用范围:

NSRange easternRange = NSMakeRange (80, 20); 
NSRange southernRange = NSMakeRange (170, 20); 

NSInteger heading = 92;
if (NSLocationInRange(heading,easternRange)) {
  NSLog(@"Heading Easterly.");
} else if (NSLocationInRange(heading,southernRange)) {
  NSLog(@"Heading southerly.");
}

等等。

如果您希望更全面地使用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;

在Cocoa和Cocoa Touch中,使用独立于体系结构的NSInteger和NSInteger通常比使用int要好。@Chuck:对。但是OP使用了
+numberwhithint:
,所以我在代码中保留了
int
。谢谢,@KennyTM。现在干净多了@查克-接受了你的建议,也使用了它。我最初使用numberWithInt:因为它没有引发异常,所以我保留了它。@Chuck NSDecimalNumber在处理对十进制值敏感的数值时也很有用,就像在处理货币时一样。签出此:
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;