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!
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
}
-(BOOL)isANumber:(NSString *)string{
NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^[0-9]+$'"];
return [numberPredicate evaluateWithObject:string];
}