Objective c: Check if integer/int/number

前端 未结 6 2133
不知归路
不知归路 2021-02-13 02:25

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

6条回答
  •  野的像风
    2021-02-13 03:11

    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
    }
    

提交回复
热议问题