11

Possible Duplicate:
iPhone how to check that a string is numeric only

I am trying to look up some basic functionality in Objective C, but can't seem to locate the basic answer.

What I really need is to check

if ( some_string is numeric )
{

}

I would assume there would just be some built in function for that. No?

I am looking at this StackOverflow question How to check if NSString is numeric or not and they seem to be doing things the very complicated way.

Isn't there some basic check I can do in one simple line?

Thanks!

Community
  • 1
  • 1
GeekedOut
  • 16,905
  • 37
  • 107
  • 185

1 Answers1

39

Isn't there some basic check I can do in one simple line?

No.

NSScanner *scanner = [NSScanner scannerWithString:testString];
BOOL isNumeric = [scanner scanInteger:NULL] && [scanner isAtEnd];

If you want to allow decimal digits exchange scanInteger: by scanDouble:.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200