Possible Duplicates:
When to use NSInteger vs int?
Why is there is an NSInteger?
Can we use int
and NSInteger
interchangably? Is there any specific situation to use NSInteger
only, instead of using int
?
Can we use int and NSInteger interchangably?
No. On the LP64 architecture used by Apple, for modern OS X Cocoa, NSInteger is 64 bits wide. This means that if you cast an NSInteger to an int, comparisons against NSNotFound may fail. Here's an example:
NSRange theRange = [@"foo" rangeOfString @"x"];
int location = theRange.location;
if (location == NSNotFound) // comparison is broken due to truncation in line above
{
// x not in foo
}
In my opinion, you should only use NSInteger
where you need to pass a parameter to Cocoa or receive a result from Cocoa and the documentation says the data type is NSInteger
. In all other cases:
int
or long
.stdint.h
types e.g. int32_t
, int64_t
. intptr_t
or uintptr_t