Question

Looking in the headers, I can see that for the 64-bit system I'm compiling for, both NSUInteger and UInt32 are defined as 32 bit unsigned integers, so why is Xcode claiming a loss in precision in a warning when I assign one to the other?

"Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'UInt32' (aka 'unsigned int')"

I can see that the warning is referring to one as 'long' and one as 'int', but since they both resolve to the same 32 bit unsigned integer on this system, there should be no loss in precision.

It's not actually causing any problems, but it seems I've probably misunderstood something so would like to better understand.

There was this question, but it addresses safety as opposed to 'why': Can I safely store UInt32 to NSUInteger?

Was it helpful?

Solution 2

NSUInteger and NSInteger can be used even in iOS as well as in OSX.

#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

iOS are 32 bit device whereas OSX is 64 bit(Earlier OSX were 32 bit and even you can opt to make an application for 32 bit OS), therefore it contains an if-else directive based on which it is set.

In OSX I always use %ld with NSInteger but in iOS it has to be %d.

If you use %d for NSInteger it shows the warning Loses integer precision.

OTHER TIPS

On the 64-bit OS X platform:

  • NSUInteger is defined as unsigned long, and
  • unsigned long is a 64-bit unsigned integer.

So UInt32 and NSUInteger do not have the same size, which explains the compiler warning.

See also NSUInteger in the "Foundation Data Types Reference":

When building 32-bit applications, NSUInteger is a 32-bit unsigned integer. A 64-bit application treats NSUInteger as a 64-bit unsigned integer.

Remark: 64-bit OS X uses (as many Unix and Unix-like systems) the LP64 data model, which means that long and pointers have 64-bit. 64-bit Windows uses the IL32P64 data model, where long has 32-bit and pointers have 64-bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top