 |
 |
When to use NSNumber?
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
I'm a bit confused as to the purpose of this class.
When is it better to use NSNumber instead of int or float or whatever?
Are there times when it is better to just use the C types? (i.e. int, float, etc).
Can anyone please clarify this?
thanks,
-N
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
it's darn useful for converting NSStrings to int/float/bool and vice versa. As in,
NSLog([[NSNumber numberWithInt:myValue]stringValue); //logs a C value
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status:
Offline
|
|
I'd been using [NSString stringWithFormat:] for that...
anyway, the real need for NSNumber is for storing numbers in other objects (like NSArray, NSDictionary, NSUserDefaults, etc) that only take NSObjects as input
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
Aha - that's very useful indeed!
But then there's the frustration of comparing the value of an NSNumber to a constant (i.e. it's so much more darn complex than "if (i > 0)" b/c you have to either load the constant into an NSNumber or extract a C type from the variable)....
So should one generally use NSNumber rather than C types for instance variables? Or is there a rule of thumb for deciding these things?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jan 2002
Location: NYC
Status:
Offline
|
|
When to use NSNumber:
1) Directly manipulating numbers that need to also be onscreen in some sort of collection class.
For example, if you have an NSTableView that shows people with thier names, heights and ages, you might be able to get away with a string as height, but the age would have to be a number, and the Cocoa collection classes require NSObjects. Also if you made the height a NSNumber than you could use an NSFormatter to format the height in a meaningful way.
2) The NSNumber is of actual value to you outside of your class for more than trivial comparisons.
The datasource mentioned in the previous example is one case where you actually need to maintain an actual instance an NSNumber, another is when using methods that require them. I can't think of any off the top of my head unfortunately but there are some that perform operations solely on NSNumbers.
3) Memory
No, NSNumbers aren't a smaller memory footprint than a basic int, float, or double, however when you have a View Object that needs NSNumbers it is more efficient to retain your own than to make temporary ones of your primitives each time one is requested.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2003
Status:
Offline
|
|
Thanks for the help, everyone - I think it makes more sense to me now.
-N
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
Also, the NSDecimalNumber class can sometimes be useful for doing arithmetic operations on numbers that can't be represented by primitive data types, such as really large integers.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|