NSInteger vs. NSNumber

Joined
Oct 4, 2011
Messages
6
Reaction score
0
Points
1
Tell me please in what cases it would be better to use NSInteger type and in what cases NSNumber*?
 
OP
S
Joined
Oct 4, 2011
Messages
6
Reaction score
0
Points
1
int vs. NSNumber

What is the difference benween int and NSNumber? Is NSNumber an analog of th object type in C#?
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
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:

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. (Note that number objects do not necessarily preserve the type they are created with.) It also defines a compare: method to determine the ordering of two NSNumber objects.

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.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
Int is a scalar data type that is part of C. NSNumber is an objective C class that is a subclass of NSValue. NSValue is designed to store non-objective-c classes (ie: pointers, char, int, NSRange (which is a struct), CGSize (struct), CGRect (struct), etc. - see Apple docs for all of the types of data an NSValue object can store).

NSNumber is a specific subclass of NSValue - it's sole purpose is to deal with numeric scalar C data types (ie: int, float, double, etc.).

I can't compare it to C# as I don't know C#.
 

BrianLachoreVPI


Retired Staff
Joined
Feb 24, 2011
Messages
3,733
Reaction score
124
Points
63
Location
Maryland
Your Mac's Specs
March 2011 15" MBP 2.3GHz i7 Quad Core 8GB Ram | Mid 2011 27" iMac 3.4 GHz i7 16 GB RAM 2 TB HDD
I've merged your two threads. Please don't double post. Thanks.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top