NSInteger and NSUInteger

suggest change

NSInteger is a typedef for int or long, depending on the architecture. NSUInteger is the corresponding unsigned typedef. The exact preprocessor condition varies by SDK. A typical header looks like this:

#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

A signed integer can be negative. A 32-bit int ranges from -2,147,483,648 to 2,147,483,647, while a 32-bit unsigned int ranges from 0 to 4,294,967,295. The extra positive range comes from using the sign bit as magnitude. On 64-bit architectures, long and NSInteger are much wider.

Most Apple methods return NSInteger or NSUInteger rather than int. Assigning an NSInteger to an int warns on 64-bit, because the value may not fit. For example, count on an array returns NSUInteger.

NSNumber *iAmNumber = @0;

NSInteger iAmSigned = [iAmNumber integerValue];
NSUInteger iAmUnsigned = [iAmNumber unsignedIntegerValue];

NSLog(@"%ld", (long)iAmSigned); // Print an NSInteger.
NSLog(@"%lu", (unsigned long)iAmUnsigned); // Print an NSUInteger.

Like BOOL, NSInteger and NSUInteger are primitive types. Wrap one in an NSNumber with @ (as above) or with these methods:

[NSNumber numberWithInteger:0];
[NSNumber numberWithUnsignedInteger:0];

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents