Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > When to use NSNumber?

When to use NSNumber?
Thread Tools
nufferkay
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Dec 10, 2003, 11:12 PM
 
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
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Dec 11, 2003, 12:26 AM
 
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
     
Uncle Skeleton
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
Dec 11, 2003, 12:34 AM
 
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
     
nufferkay  (op)
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Dec 11, 2003, 12:38 AM
 
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?
     
K++
Senior User
Join Date: Jan 2002
Location: NYC
Status: Offline
Reply With Quote
Dec 11, 2003, 01:27 AM
 
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.
     
nufferkay  (op)
Fresh-Faced Recruit
Join Date: Dec 2003
Status: Offline
Reply With Quote
Dec 11, 2003, 06:52 PM
 
Thanks for the help, everyone - I think it makes more sense to me now.

-N
     
Rickster
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Dec 12, 2003, 06:30 PM
 
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.
Rick Roe
icons.cx | weblog
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:34 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,