开发者

Difference between int, NSInteger and NSUInteger

开发者 https://www.devze.com 2023-04-09 18:35 出处:网络
What is the main difference between int, NSInteger and NSUInteger in Obje开发者_如何学编程ctive-C?

What is the main difference between int, NSInteger and NSUInteger in Obje开发者_如何学编程ctive-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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号