Objective-C implicit conversion loses integer precision ‘NSUInteger’ (aka ‘unsigned long’) to ‘int’ warning

The count method of NSArray returns an NSUInteger, and on the 64-bit OS X platform

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

So int is a “smaller” datatype than NSUInteger, therefore 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.

To fix that compiler warning, you can either declare the local count variable as

NSUInteger count;

or (if you are sure that your array will never contain more than 2^31-1 elements!),
add an explicit cast:

int count = (int)[myColors count];

Leave a Comment