- (BOOL)isPureFloat:(NSString *)string NSScanner* scan = [NSScanner scannerWithString:string]; float val; return [scan scanFloat:&val] && [scan isAtEnd]; 第二种方式是使用循环判断 - (BOOL)isPureNumandCharacters:(NSString *)text for(int i = 0; i < [text length]; ++i) int a = [text characterAtIndex:i]; if ([self isNum:a]) continue; else return NO; return YES; 或者 C语言中常用的方式. - (BOOL)isAllNum:(NSString *)string unichar c; for (int i=0; i<string.length; i++) c=[string characterAtIndex:i]; if (!isdigit(c)) return NO; return YES; 第三种方式则是使用NSString的trimming方法 - (BOOL)isPureNumandCharacters:(NSString *)string string = [string stringByTrimmingCharactersInSet;[NSCharacterSet decimalDigitCharacterSet]]; if(string.length > 0) return NO; return YES; |