Can I safely store UInt32 in NSUInteger?

In the header, it is defined as:

#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

So, does UInt32 work without problems in NSUInteger (unsigned int)? Where is the difference between UInt32 and unsigned int?

And I guess unsigned long is more than unsigned int?

+3
source share
2 answers

I believe that the only difference between UInt32and unsigned intis that a is UInt32guaranteed to have a length of 32 bits, while unsigned inttechnically it may be shorter if you were running (say) a <32-bit operating system.

, , Mac iPhone 32- , unsigned int, UInt32 NSUInteger . , NSUInteger 64 ( Mac x86_64).

unsigned long unsigned int, UInt32 tpedef'd unsigned long, , . unsigned long unsigned int.

+9

, :

 if (sizeof(NSUInteger) != sizeof(UInt32)) {
     printf("Error: Wrong integer type size!\n");
     exit(0);
 }
+1

Source: https://habr.com/ru/post/1743711/


All Articles