tags:

views:

58

answers:

2

class or instance method for checking string is valid integer?

+2  A: 

You can use NSScanner for that. NSString itself also has -doubleValue and -intValue methods.

Vladimir
I'd say that NSScanner would be the preferred way, as -scanDouble: returns YES or NO depending on whether the string has a valid floating point number within it or not.
Brad Larson
A: 

If you're looking at the ENTIRE string, rather than just a portion, you could just use -intValue or -doubleValue. If it returns non-zero, it's valid.

Ben Gottlieb
But what if the string is actually "0" or "0.0"? Admittedly, checking for a non-zero number can be good enough for many cases.
Brad Larson