Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
iOS Development
NSInteger vs. NSNumber
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Nethfel" data-source="post: 1304851" data-attributes="member: 89124"><p>NSInteger is just a typedef of a long int. NSNumber is an objective c class for storing and accessing any C scalar type (including ints, floats, char, etc.). Here's the definition of an NSNumber from Apples docs:</p><p></p><p></p><p></p><p>Which is better to use what? If you need a counter, or if you need to reference a tag, or you need to do basic math an NSInteger is usually easier to deal with. If you need to store the value in one of the objective c collections or utilize the value with something that needs or is looking for an NSNumber, or use serialization or as a dictionary key, use an NSNumber (Obj-C collections like NSArray, NSDictionary, NSSet, etc. can only store objects, it can't store scalar data types). If you need to change the value easily (add/subtract/multiply/divide/etc.) use a scalar type. NSNumbers are immutable and can't be changed when set. </p><p></p><p>Personally I just use what I need for when I feel its necessary. Sometimes it's an int/NSInteger/float/double/etc. and sometimes it's a NSNumber. I use scalar types much more often.</p></blockquote><p></p>
[QUOTE="Nethfel, post: 1304851, member: 89124"] NSInteger is just a typedef of a long int. NSNumber is an objective c class for storing and accessing any C scalar type (including ints, floats, char, etc.). Here's the definition of an NSNumber from Apples docs: Which is better to use what? If you need a counter, or if you need to reference a tag, or you need to do basic math an NSInteger is usually easier to deal with. If you need to store the value in one of the objective c collections or utilize the value with something that needs or is looking for an NSNumber, or use serialization or as a dictionary key, use an NSNumber (Obj-C collections like NSArray, NSDictionary, NSSet, etc. can only store objects, it can't store scalar data types). If you need to change the value easily (add/subtract/multiply/divide/etc.) use a scalar type. NSNumbers are immutable and can't be changed when set. Personally I just use what I need for when I feel its necessary. Sometimes it's an int/NSInteger/float/double/etc. and sometimes it's a NSNumber. I use scalar types much more often. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
iOS Development
NSInteger vs. NSNumber
Top