本文介绍了如何将NSRange与整数一起使用来简化我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Objective-C并制作了一个小指南针应用程序,当它落入一系列标题时会显示方向。它运行得很好,但我想知道是否有一种更简洁的方式使用 NSRange 来编写它。经过大量的研究,似乎 NSRange 更多地用于字符串函数而不是数字。

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.

我试图制作一个 NSRange 的实例我的起点让这个更加简洁,我无法追踪如果一个数字落在 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.

我在这里是正确的轨道,或者我是否让它比它需要的更冗长?

Am I on the right track here, or am I making this more verbose than it needs to be?

提前致谢..

这是我尝试缩短代码时失败了:

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:@"-"];

}


推荐答案

是的。如果要进行数值运算,请避免使用NSNumber。 NSNumber类的存在只是因为像NSArray,NSDictionary等的Objective-C集合只能容纳Objective-C对象。否则,你应该总是使用普通的 int NSInteger CGFloat double 等。

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.

您不需要使用NSRange。

You don't need to use NSRange.

这篇关于如何将NSRange与整数一起使用来简化我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:48