On 02/04/2009, at 12:17 PM, Greg Robertson wrote:

I would like to convert an NSString to an NSNumber. Is there a direct
method for this or should I go NSString to double and then double to
NSNumber?


One reason this isn't directly supported is probably because it only has meaning within a very narrow scope. What is the numeric value of "steve" for example? It hasn't got one of course. So you have to be sure that your string does represent a number, which most strings do not.

If you know your string contains a simple numeric constant (and only this), the method others have pointed out will work.

Another class of strings do have meaningful numeric conversions also, such as "3 + 4" or "sin(45)". If this is what you're interested in, there is no built-in support for parsing this sort of string content, but it can be done - for example check out:

http://apptree.net/parser.htm

If you're simply after accepting a numeric value input by the user, using an NSTextField with a suitable formatter then requesting its - doubleValue: will return an NSNumber. Using a formatter allows you to validate the input before the value gets passed on to other code.

--Graham


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to