If NSInteger
is just like a regular int
then why does it exist and what is its purpose in being called NSInteger
?
I'm new to Mac OS X programming and we'll be having a report for this.
NSInteger is a type definition that describes an integer - but it is NOT equivalent to int on 64-bit platforms. You can examine the typedef by cmd-clicking on NSInteger in Xcode. NSInteger is defined as int when building a 32-bit app and as long for 64-bit apps.
The purpose of NSNumber is simply to box primitive types in objects (pointer types), so you can use them in situations that require pointer-type values to work. One common example: you have to use NSNumber if you want to persist numeric values in Core Data entities.
As said by others before, NSNumber is an NSObject subclass. It is not a C primitive (like int, unsigned int, float, double, etc.) NSInteger , CGFloat , NSUInteger are simple typedefs over the C primitives. The need for NSNumber arises from the need to use numbers as parameters to APIs that require Objects.
NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .
It's an architecture-safe (64 vs 32 bit) type to support different platforms and implementations of C.
Apple recommends that you use NSInteger
over normal types anyway, I would assume for portability!
You can read more at this Foundation Types Reference.
Basic description:
When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer.
Other than the typedef being different on different systems (long
on 64-bit systems, int
on 32-bit), there isn't much of a reason.
Arguably, it gives the impression that an NSInteger
is an object, when it's not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With