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 > NSNumberWithString?

NSNumberWithString?
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 1, 2002, 07:02 PM
 
I am trying to make an NSNumber from a string. I noticed that there is an NSDecimarNumberWithString method, but nothing analogous for NSNumber. Is there an easy way to do it?

thanks,
kman
     
Junior Member
Join Date: Mar 2002
Location: Maryland
Status: Offline
Reply With Quote
Apr 1, 2002, 08:31 PM
 
You can do a:

[NSNumber numberWithInt:[theString intValue]];


Not sure if it's the most efficient means though
Adam Iser
AdiumX.com
     
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
Apr 1, 2002, 09:59 PM
 
Read the docs for NSScanner. It does what you want.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 1, 2002, 10:11 PM
 
On a larger note. I guess I don't really understand when I would want to use an int v. an NSNumber. I don't seem to be able to do arithmetic with NSNumbers. Maybe I'm just going about it wrong.

kman
     
Junior Member
Join Date: Mar 2002
Location: Maryland
Status: Offline
Reply With Quote
Apr 1, 2002, 10:41 PM
 
NSNumbers are really only useful when you're building an NSArray, putting keys in a dictionary (like a preference file), or other very cocoa-y things.

In those cases, Apple's functions have been set up to deal only with classes. Any function that wants a type of (id) can only be passed a class.

So the problem comes in where you need to get an integer in there. NSNumber is really just a "wrapper" around a regular int/float/double variable. So you "hide" your int inside an NSNumber and pass it to the function. The same kind of deal happens with NSStrings, which can be thought of as a simple wrapper around a standard c string.

Most of the time you'll be using regular old C/C++ style ints.


Let me know if I've completely confused you
Adam Iser
AdiumX.com
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 1, 2002, 10:48 PM
 
Nevermind. I just figured out that I have to message the NSNumber with intValue.

edit: I was obviously posting this just as reslmadA was giving his wonderful description.


kman

[ 04-02-2002: Message edited by: kman42 ]
     
   
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:29 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2