objective-c NSNumber 和 NSInteger 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1285098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between NSNumber and NSInteger?
提问by mk12
What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats?
NSNumber 和 NSInteger 有什么区别?是否有更多这样的原语我应该知道?有花车吗?
采纳答案by Tyler
The existing answers are useful; adding to them:
现有的答案很有用;添加到他们:
Yes, NSUIntegergives twice the range among positive integers as NSInteger, but I think another critical reason to choose between the two is simply to distinguish among cases where negative values simply do not make sense.
是的,NSUInteger给出正整数范围的两倍NSInteger,但我认为在两者之间进行选择的另一个关键原因只是为了区分负值根本没有意义的情况。
Example:the return value of NSArray's countmethod is an NSUInteger, which makes sense since we cannot have an array with a negative number of elements. When the coder knows it's unsigned, he/she has more freedom to perform operations that might be unreliable in the signed case, including bitwise operations such as shifting. This blog posttalks more about signed vs unsigned.
示例:NSArray的count方法的返回值是 an NSUInteger,这是有道理的,因为我们不能拥有包含负数元素的数组。当编码器知道它是无符号的时,他/她可以更自由地执行在有符号情况下可能不可靠的操作,包括按位操作,例如移位。 这篇博文更多地讨论了签名与未签名。
My guess about CGFloatvs NSFloat(which doesn't exist): It might be the case that the designers of the NeXTStep-named code elements simply didn't need to specify too many float values, outside of time intervals. So they gave NSTimeInterval, which is a floating-point type, but that is clearly intended for use with time intervals. And, frankly, it's great to have that type because you know it's always meant to be in seconds without having to deal with a struct. When you move into the graphics world (where Core Graphics lives), suddenly floats are all around you, hovering in the air (haha). So it makes sense to introduce a CGFloatthere. This paragraph is all "educated speculation."
我对CGFloatvs 的NSFloat猜测(不存在):可能是这样的情况,以 NeXTStep 命名的代码元素的设计者根本不需要在时间间隔之外指定太多的浮点值。所以他们给出了NSTimeInterval,这是一种浮点类型,但这显然是为了与时间间隔一起使用。而且,坦率地说,拥有这种类型很棒,因为您知道它总是意味着在几秒钟内无需处理结构。当您进入图形世界(Core Graphics 所在的地方)时,您的周围会突然出现漂浮物,盘旋在空中(哈哈)。所以在CGFloat那里引入 a 是有道理的。这一段全是“有根据的推测”。
Also, just to be clear about why you might use NSIntegeretc instead of primitive types: Because this way it's easier to write portable code that takes full advantage of the machine architecture. For example, a CGFloatuses 32 bits on some machines and 64 bits on others, depending largely on how much of an efficiency gap there is on those machines for those sizes. In some cases, there's no real speedup for using 32 bits vs 64 bits, so you might as well use 64 bits. If you've declared things as CGFloat's, you suddenly get that extra precision "for free" when you recompile.
另外,要清楚为什么您可能会使用NSIntegeretc 而不是原始类型:因为这样可以更容易地编写充分利用机器架构的可移植代码。例如,aCGFloat在某些机器上使用 32 位,而在其他机器上使用 64 位,这在很大程度上取决于这些机器上对于这些大小的效率差距有多大。在某些情况下,使用 32 位与 64 位并没有真正的加速,因此您不妨使用 64 位。如果你已经将事物声明为CGFloat's ,那么当你重新编译时,你会突然“免费”获得额外的精度。
And, as iKenndac has pointed out, NSNumberis a wrapper class for allof these (and other primitive or quasi-primitive types like the BOOL) which enables you to include it in your NSArrays and NSDictionarys, archive them more easily, and do other things that NSObjects can do.
而且,正如iKenndac所指出的,NSNumber是所有的包装类的这些(和其他原始或半原始类型一样BOOL),它使您能够将其包含在NSArrayS和NSDictionaryS,存档他们更容易,做其他的事情,NSObject可以做。
回答by iKenndac
NSNumberis a class, not a primitive, and is used when you need to put raw numbers into dictionaries, arrays, or otherwise encapsulate them. NSInteger, NSUInteger, CGFloat, etc are simple types and correspond (on 32-bt systems like the iPhone) to int, unsigned intand float.
NSNumber是一个类,而不是基元,当您需要将原始数字放入字典、数组或以其他方式封装它们时使用。NSInteger、NSUInteger、CGFloat等是简单类型,对应(在 iPhone 等 32-bt 系统上)对应于int、unsigned int和float。
As a general rule, if you need to store a number somewhere, use NSNumber. If you're doing calculations, loops, etc, use NSInteger, NSUIntegeror CGFloat.
作为一般规则,如果您需要在某处存储数字,请使用NSNumber. 如果您要进行计算、循环等,请使用NSInteger,NSUInteger或CGFloat。
You can wrap an NSIntegerinto an NSNumber:
您可以将 an 包装NSInteger成 an NSNumber:
NSNumber *aNumber = [NSNumber numberWithInteger:21];
... and get it back:
...并取回:
NSInteger anInteger = [aNumber integerValue];
You can find out more info here: http://iosdevelopertips.com/cocoa/nsnumber-and-nsinteger.html
你可以在这里找到更多信息:http: //iosdevelopertips.com/cocoa/nsnumber-and-nsinteger.html
回答by Jason
NSInteger is just like a traditional intin C. It's a typedef. There are others like NSUInteger, CGFloat, etc. that all are synonyms for primitive types.
NSInteger 就像intC 中的传统。它是一个 typedef。还有其他像NSUInteger,CGFloat等,它们都是原始类型的同义词。
NSNumber is useful when you need to stick a number into an NSArray or NSDictionary. The standard practice is to use these collections versus rolling your own; the drawback is that they can only contain Objective-C objects.
当您需要将数字粘贴到 NSArray 或 NSDictionary 中时,NSNumber 很有用。标准做法是使用这些集合而不是滚动自己的集合;缺点是它们只能包含 Objective-C 对象。
NSNumber essentially wraps an int(or float, etc) into an Obj-C object (similar to C#/Java's concept of 'boxing' a primitive type) that can be added to an array:
NSNumber 本质上将一个int(或浮点数等)包装到一个可以添加到数组中的 Obj-C 对象(类似于 C#/Java 的“装箱”原始类型的概念):
NSArray * myArray = [[NSArray alloc] init];
[myArray addObject:3]; // invalid, will not compile.
[myArray [NSNumber numberWithInt:3]]; // ok
For performance reasons, if you can, use primitive types (like int, float, int[]). However, sometimes you cannot avoid NSArray/NSNumber, such as when you are reading or writing entries into a .plistfile.
出于性能原因,如果可以,请使用原始类型(如 int、float、int[])。但是,有时您无法避免使用 NSArray/NSNumber,例如当您在.plist文件中读取或写入条目时。
回答by eden
NSNumber is generally used for storing variables, NSUInteger is used for arithmetic
NSNumber 一般用于存储变量,NSUInteger 用于算术
you can change a NSNumber to a NSUInteger by doing this:
您可以通过执行以下操作将 NSNumber 更改为 NSUInteger:
NSNumber *variable = @1;
NSUInteger variableInt = [variable unsignedIntegerValue];
It has been said before but as a general rule of thumb, variables that need to be defined with * are Objective C classes, whereas variables that do not need a * are C primitives.
之前已经说过,但作为一般经验法则,需要用 * 定义的变量是 Objective C 类,而不需要 * 的变量是 C 原语。
回答by Motti Shneor
As said by others before, NSNumberis an NSObjectsubclass. It is not a C primitive (like int, unsigned int, float, double, etc.) NSInteger, CGFloat, NSUIntegerare simple typedefsover the C primitives.
正如之前其他人所说,NSNumber是一个NSObject子类。它不是一个原语C(如int,无符号整型,浮点,双精度等)NSInteger,CGFloat,NSUInteger是简单的typedefs开始,经过C基元。
The need for NSNumberarises from the need to use numbers as parameters to APIs that require Objects. Example is, when you want to store a number in an NSArray, In Core-Data, or in an NSDictionary.
需要NSNumber使用数字作为需要对象的 API 的参数的需要。例如,当您想将数字存储在NSArray、In Core-Data 或NSDictionary.
Are there more primitives like these that I should know about? Well, NSNumberis not a primitive type, and it wraps around all kinds of numbers (floats, integral types, booleans and so on). You should also learn about NSValue, which is the base for NSNumber.
是否有更多这样的原语我应该知道?嗯,NSNumber不是原始类型,它包含各种数字(浮点数、整数类型、布尔值等)。您还应该了解 NSValue,它是NSNumber.
Is there one for floats? There is no "NS" typedef for float, but NSNumbercan wrap around any float number:
有花车吗?浮点数没有“NS”typedef,但NSNumber可以环绕任何浮点数:
NSNumber *myPi = [NSNumber numberWithFloat:3.1415926];
Or you could use the CoreGraphics primitive type CGFloat.
或者您可以使用 CoreGraphics 原始类型 CGFloat。

