monkeyboy monkeyboy - 1 year ago 204
Objective-C Question

objective c implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' warning

I'm working through some exercises and have got a warning that states:

implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int'

I'm quite the noob and would appreciate any help.. thanks.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

@autoreleasepool {

NSArray *myColors;

int i;
int count;

myColors = @[@"Red", @"Green", @"Blue", @"Yellow"];

count = myColors.count; // <<< issue warning here

for (i = 0; i < count; i++)

NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);


return 0;



Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download