2

Possible Duplicate:
NSString is integer?

I'm looking to see if a string is ONLY numbers. Basically:

if (string is integer):
   #do whatever
else:
   #error

How would I do this? Thanks!

Community
  • 1
  • 1
jsttn
  • 1,495
  • 3
  • 16
  • 21

2 Answers2

5

I have used [NSCharacterSet decimalDigitCharacterSet]. Definitely works well.

NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
Legolas
  • 12,145
  • 12
  • 79
  • 132
0
-(BOOL)isANumber:(NSString *)string{
    NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^[0-9]+$'"];
    return [numberPredicate evaluateWithObject:string];
}
Carmen
  • 6,177
  • 1
  • 35
  • 40
Jesse Gumpo
  • 4,777
  • 1
  • 20
  • 29