简体   繁体   English

int,NSInteger和NSUInteger之间的区别

[英]Difference between int, NSInteger and NSUInteger

What is the main difference between int , NSInteger and NSUInteger in Objective-C? 之间的主要区别是什么intNSIntegerNSUInteger在Objective-C?

Which one is better to use in an application and why? 哪一个更适合在应用程序中使用?为什么?

In such cases you might right click and go to definition: 在这种情况下,您可以右键单击并转到定义:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

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. 现在我们不必担心int的大小或指针在任何特定硬件上有多大。

"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). “C”在这方面很糟糕,只说明long至少与int一样大,int是硬件的“自然”整数大小(无论这意味着什么),int至少与int一样长短 - 一个(大混乱)。

This seemed like a good idea at the time coming from Fortran, but did not age well. 这在Fortran的时候似乎是一个好主意,但没有成熟。

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. 可以使用POSIX定义,比如uint32_t,int16_t等。但是这并没有解决指针在任何特定硬件上需要多大的问题。

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. 因此,如果Apple将返回类型定义为NSUInteger,则只需使用它,您无需知道特定硬件的大小是16位,32位还是64位。 (I picked those values out-of-the-air just for an example). (我只是为了一个例子,我在无线中选择了这些值)。

As you can see in @Bastian the actual size depends on hardware. 正如你在@Bastian中看到的那样,实际大小取决于硬件。

The documentation answers the "letter of the question" but does not provide an understanding of "why"? 文档回答了“问题的信”,但没有提供对“为什么”的理解?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM