• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Looking for [NSNumber numberWithString:]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Looking for [NSNumber numberWithString:] (From: Greg Robertson <email@hidden>)

  • Prev by Date: Re: Huge and weird problem with NSScanner
  • Next by Date: Re: NSPopUpButton pullsDown:YES and dummy first item - normal?
  • Previous by thread: Re: Looking for [NSNumber numberWithString:]
  • Next by thread: Re: Looking for [NSNumber numberWithString:]
  • Index(es):
    • Date
    • Thread