Re: Looking for [NSNumber numberWithString:]
Re: Looking for [NSNumber numberWithString:]
- Subject: Re: Looking for [NSNumber numberWithString:]
- From: Graham Cox <email@hidden>
- Date: Thu, 2 Apr 2009 22:51:42 +1100
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@hidden)
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:
This email sent to email@hidden