NSNumber和NSInteger有什么区别?(What's the difference between NSNumber and NSInteger?)

编程入门 行业动态 更新时间:2024-10-27 14:23:15
NSNumber和NSInteger有什么区别?(What's the difference between NSNumber and 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?

最满意答案

现有的答案是有用的; 加入他们:

是的, NSUInteger将正整数之间的范围提高为NSInteger两倍,但是我认为在两者之间选择的另一个关键原因是简单地区分负值只是没有意义的情况

示例: NSArray的count方法的返回值是一个NSUInteger ,这是有道理的,因为我们不能有一个数目为负数的元素。 当编码器知道它是无符号的时候,他/她有更多的自由来执行在签名案例中可能不可靠的操作,包括像位移这样的按位操作。 这个博客文章谈到有关签名vs无符号的更多信息。

我对CGFloat与NSFloat猜测(不存在):NeXTStep命名代码元素的设计者可能不需要在时间间隔之外指定太多的浮点值。 所以他们给出了NSTimeInterval ,它是一个浮点型,但是它明确地用于时间间隔。 而且,坦白说,这是非常好的,因为你知道它总是在几秒钟内无需处理一个结构体。 当您进入图形世界(Core Graphics所在的地方)时,突然浮动在你周围,徘徊在空中(哈哈)。 所以在那里介绍一个CGFloat是有道理的。 这一段都是“受过教育的猜测”。

另外,为了清楚你为什么可以使用NSInteger等而不是原始类型 :因为这样,编写可以充分利用机器架构的便携式代码更容易。 例如, CGFloat在一些机器上使用32位,在其他机器上使用64位,这在很大程度上取决于这些尺寸的这些机器上的效率差距。 在某些情况下,使用32位和64位都没有真正的加速,所以你也可以使用64位。 如果您已经声明为CGFloat ,那么当您重新编译时,您会突然获得额外的精确度“免费”。

而且,正如iKenndac所指出的那样, NSNumber是所有这些(以及其他原始或类似原始类型的BOOL ) 的封装类,它使您能够将其包含在NSArray和NSDictionary ,更容易归档它们,做其他事情, NSObject可以做。

The existing answers are useful; adding to them:

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.

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.

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."

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.

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.

更多推荐

本文发布于:2023-08-07 14:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   NSNumber   NSInteger   difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!