Objective c: Check if integer/int/number

Daniel picture Daniel · Aug 13, 2010 · Viewed 35.9k times · Source

In objective c, how can i check if a string/NSNumber is an integer or int

Answer

Anshu Chimala picture Anshu Chimala · Aug 13, 2010

If you're trying to determine whether or not an NSString has a numeric value or not, try using NSNumberFormatter.

-(BOOL) stringIsNumeric:(NSString *) str {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *number = [formatter numberFromString:str];
    [formatter release];
    return !!number; // If the string is not numeric, number will be nil
}