views:

79

answers:

1

Possible Duplicate:
NSString is integer?

I've been searching around for the last half hour trying to find an easy way to convert an instance of NSString into an integer, and to be able to tell if the string was validly formatted. So far, I haven't been able to find anything. There's the intValue and integerValue methods in NSString, but these just return 0 if invalidly formatted. In my case, 0 is a valid number. There's also NSNumberFormatter, which will parse a decimal number without a problem, but as far as I can tell it doesn't do integers, although I might just be missing something.

It seems like this is too simple of a problem to be omitted from Foundation. I'd appreciate somebody pointing me in the right direction.

+1  A: 

See this post: http://stackoverflow.com/questions/565696/nsstring-is-integer

John JJ Curtis
Awesome. The first answer is exactly what I needed. Thanks for the help.
helixed
Please use comments to point out duplicates. With a reputation of 3,000 you can also vote to close as a duplicate using the *"close"* link below the question.
Georg Fritzsche
One thing I did notice from testing is if the string contains something like "123adf", 123 is returned. Is there any way to get the scanner to indicate this is an invalid integer?
helixed
Nevermind, I figured it out. You have to use the isAtEnd NSScanner method.
helixed