int、NSInteger 和 NSUInteger 之间的区别
Objective-C 中 int
、NSInteger
和 NSUInteger
之间的主要区别是什么?
哪一个更适合在应用程序中使用?为什么?
What is the main difference between int
, NSInteger
and NSUInteger
in Objective-C?
Which one is better to use in an application and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,您可以右键单击并转到定义:
In such cases you might right click and go to definition:
区别在于抽象类型及其从硬件调整大小的关联。现在我们不必担心 int 的大小或它的指针在任何特定硬件上有多大。
“C”在这方面很糟糕,只是声明 long 至少与 int 一样大,int 是硬件的“自然”整数大小(无论这意味着什么),int 至少与 int 一样长一个短——一个(一团糟)。
这在当时看来是一个来自 Fortran 的好主意,但并没有过时。
人们可以使用 POSIX 定义,例如 uint32_t、int16_t 等。但这也没有解决任何特定硬件上的指针需要多大的问题。
因此,如果 Apple 将返回类型定义为 NSUInteger,您只需使用它,而无需知道它的大小对于您的特定硬件来说是 16 位、32 位还是 64 位。 (我凭空挑选这些值只是为了举例)。
正如您在@Bastian 中看到的,实际大小取决于硬件。
该文档回答了“问题的字母”,但没有提供对“为什么”的理解?
The difference is for abstract types and their associates sized from hardware. In a manner that now we don't have to worry about what size an int is or how big it's pointer is on any particular hardware.
"C" is bad at this, only stating that a long is at least as big as an int, that an int is the "natural" integer size of the hardware (whatever that means), that an int is at least as long as a short--a (big mess).
This seemed like a good idea at the time coming from Fortran, but did not age well.
One could use the POSIX defines, things like uint32_t, int16_t, etc. But this does not address how big a pointer needs to be on any particular hardware either.
So, if Apple defines the return type to be an NSUInteger you just use that and you don't need to know if it is 16, 32 or 64 bits in size for your particular hardware. (I picked those values out-of-the-air just for an example).
As you can see in @Bastian the actual size depends on hardware.
The documentation answers the "letter of the question" but does not provide an understanding of "why"?