如何将 NSRange 与整数一起使用来简化我的代码?

发布于 2024-10-01 14:47:17 字数 2155 浏览 0 评论 0原文

我刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往事风中埋 2024-10-08 14:47:17

参加聚会迟到了,但以下方法可以工作并利用我相信的范围:

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.");
}

等等。

Coming late to the party but the following would work and make use of ranges I believe:

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.");
}

etc. etc.

清醇 2024-10-08 14:47:17

我是否让它变得比需要的更冗长?

是的。当你想要进行数值运算时,请避免使用 NSNumber。 NSNumber 类的存在只是因为像 NSArray、NSDictionary 等 Objective-C 集合只能保存 Objective-C 对象。否则,您应该始终使用普通的 intNSIntegerCGFloatdouble 等。

 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。

am I making this more verbose than it needs to be?

Yes. When you want to do numerical operations, avoid NSNumber. The NSNumber class exists only because Objective-C collections like NSArray, NSDictionary etc. can only hold Objective-C objects. Otherwise, you should always use plain int or NSInteger or CGFloat or double etc.

 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.

You don't need to use NSRange.

夏有森光若流苏 2024-10-08 14:47:17

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

A range takes a location and a length. So if you want 80 to 100 degrees for an eastern range, you could use NSMakeRange(80, 20). That would create a range starting at 80 degrees, spanning 20 degrees.

乙白 2024-10-08 14:47:17

如果您想要更集成地使用 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;

If you are wanting a more integrated use of the NSRange struct, I have found it useful for comparing portions of arrays:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文