When should I be using NSInteger
vs. int when developing for iOS? I see in the Apple sample code they use NSInteger
(or NSUInteger
) when passing a value as an argument to a function or returning a value from a function.
- (NSInteger)someFunc;...
- (void)someFuncWithInt:(NSInteger)value;...
But within a function they're just using int
to track a value
for (int i; i < something; i++)
...
int something;
something += somethingElseThatsAnInt;
...
I've read (been told) that NSInteger
is a safe way to reference an integer in either a 64-bit or 32-bit environment so why use int
at all?
Best Answer
You usually want to use
NSInteger
when you don't know what kind of processor architecture your code might run on, so you may for some reason want the largest possible integer type, which on 32 bit systems is just anint
, while on a 64-bit system it's along
.I'd stick with using
NSInteger
instead ofint
/long
unless you specifically require them.NSInteger
/NSUInteger
are defined as *dynamictypedef
*s to one of these types, and they are defined like this:With regard to the correct format specifier you should use for each of these types, see the String Programming Guide's section on Platform Dependencies