[英]How can I use NSRange with integers to simplify my code?
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. 我刚刚开始学习Objective-C并制作了一个小指南针应用程序,当它落入一系列标题时会显示方向。 It works just fine, but I wonder if there is a more concise way of writing it using NSRange
. 它工作得很好,但我想知道是否有更简洁的方法使用NSRange
编写它。 After a lot of looking, it seems like NSRange
is used more for string functions rather than numbers. 经过大量的研究,似乎NSRange
更多地用于字符串函数而不是数字。
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
. 我尝试将NSRange
一个实例作为我的起点,使其更简洁,我无法找到一个数字属于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:@"-"];
}
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. 等等
am I making this more verbose than it needs to be? 我这让它变得比它需要的更冗长吗?
Yes. 是。 When you want to do numerical operations, avoid NSNumber. 如果要进行数值运算,请避免使用NSNumber。 The NSNumber class exists only because Objective-C collections like NSArray, NSDictionary etc. can only hold Objective-C objects. NSNumber类的存在只是因为像NSArray,NSDictionary等的Objective-C集合只能保存Objective-C对象。 Otherwise, you should always use plain int
or NSInteger
or CGFloat
or double
etc. 否则,您应该始终使用plain 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.
You don't need to use NSRange. 您不需要使用NSRange。
If you are wanting a more integrated use of the NSRange struct, I have found it useful for comparing portions of arrays: 如果您想要更集成地使用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;
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). 因此,如果你想要一个东部范围80到100度,你可以使用NSMakeRange(80,20)。 That would create a range starting at 80 degrees, spanning 20 degrees. 这将创建一个范围从80度开始,跨越20度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.