NSNumber和NSInteger有什么区别? [英] What's the difference between NSNumber and NSInteger?

查看:138
本文介绍了NSNumber和NSInteger有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSNumber和NSInteger有什么区别?我应该知道更多这样的原始人吗?是否有一个浮动?

What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats?

推荐答案

现有答案很有用;添加到它们:

The existing answers are useful; adding to them:

是, NSUInteger 给出正整数范围的两倍 NSInteger ,但我认为在两者之间做出选择的另一个关键原因就是区分,其中负值根本没有意义

Yes, NSUInteger gives 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.

示例: NSArray的返回值 count 方法是一个 NSUInteger ,这是有道理的,因为我们不能有一个负数元素的数组。当编码器知道它是未签名的时,他/她有更多的自由来执行在签名情况下可能不可靠的操作,包括诸如移位之类的按位操作。 此博客文章会谈更多关于签名vs无签名。

Example: the return value of NSArray's count method 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 post talks more about signed vs unsigned.

我对 CGFloat vs NSFloat <的猜测/ code> (不存在):可能是NeXTStep命名代码元素的设计者不需要在时间间隔之外指定太多浮点值。所以他们给了 NSTimeInterval ,这是一个浮点类型,但显然是用于时间间隔。而且,坦率地说,拥有这种类型是很棒的,因为你知道它总是意味着在几秒钟内而不必处理结构。当你进入图形世界(Core Graphics所在的地方)时,突然浮在你身边,徘徊在空中(哈哈)。因此在那里引入 CGFloat 是有道理的。这一段都是受过教育的猜测。

My guess about CGFloat vs 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 CGFloat there. This paragraph is all "educated speculation."

另外,要明确为什么你可以使用 NSInteger etc而不是原始类型:因为这样编写可充分利用机器架构的可移植代码更容易。例如, CGFloat 在某些机器上使用32位,在其他机器上使用64位,这在很大程度上取决于这些机器在这些机器上的效率差距。在某些情况下,使用32位和64位没有真正的加速,所以你不妨使用64位。如果您已将事物声明为 CGFloat ,则在重新编译时,您会突然获得免费的额外精度。

Also, just to be clear about why you might use NSInteger etc 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 CGFloat uses 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.

正如iKenndac指出的那样, NSNumber 是所有这些(以及其他原始或准基本类型)的包装类 BOOL ),它允许您将其包含在 NSArray NSDictionary s,更容易归档它们,并执行 NSObject s可以做的其他事情。

And, as iKenndac has pointed out, NSNumber is a wrapper class for all of 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.

这篇关于NSNumber和NSInteger有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆