3

这种方法:

- (BOOL)checkForDigits {
    NSString *editableExpression = self.expression.text;
    for (int i = 0; i < editableExpression.length; i++){
        char charAtPosition = [editableExpression characterAtIndex:i];
        for (int c = 0; c < 10; c++ ){
            char digitAtPosition = digits[c];
            if (charAtPosition == digitAtPosition){
                return TRUE;

            }
            else {
                return FALSE;
            }
        }
    }
}

似乎总是返回 FALSE,我不知道为什么。我会使用 isnan 方法,但我试过了,但没有成功,所以我只做了自己的 isnan(实际上与 isnan 正好相反)。数字数组只是

char digits[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
4

7 回答 7

2

用这个:

- (BOOL)checkForDigits {
    char digits[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    BOOL result;
    NSString *editableExpression = @"dsjkdjk12lkv";
    for (int i = 0; i < editableExpression.length; i++){
        char charAtPosition = [editableExpression characterAtIndex:i];
        for (int c = 0; c < 10; c++ ){
            char digitAtPosition = digits[c];
            if (charAtPosition == digitAtPosition){
                NSLog(@"true");
                return TRUE;

            }
            else {
                result = FALSE;
            }
        }
    }
    return result;
}

实际上,它仅在第一个循环中返回 false。所以循环只运行了一个周期。

于 2013-08-16T13:43:06.997 回答
0

因为

charAtPosition包含字母而不是数字,例如'1', '2',......

characterAtIndex返回给定数组位置的字符。

于 2013-08-16T13:25:03.883 回答
0

可能更好的方法是使用 NSScanner?

        NSScanner *scanner = [NSScanner scannerWithString:string];
        NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

        [scanner scanCharactersFromSet:numbers intoString:&obj1String];
        obj1Num = [NSNumber numberWithInt:[obj1String integerValue]];

        if(obj1String) {

        } else {

        }
于 2013-08-16T13:28:40.077 回答
0

editableExpression不是从数字开始的。

NSString *editableExpression =@"A1" // return false

NSString *editableExpression =@"1A" // return True  

//This will return TRUE if string contain digit
- (BOOL)checkForDigits {
    NSString *editableExpression = @"1a1";
    for (int i = 0; i < editableExpression.length; i++){
        char charAtPosition = [editableExpression characterAtIndex:i];
        for (int c = 0; c < 10; c++ ){
            char digitAtPosition = digits[c];
            if (charAtPosition == digitAtPosition){
                return TRUE;
            }
        }
    }
    return FALSE;
}
于 2013-08-16T13:29:53.630 回答
0

试试这个:

NSRange match = [string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet] options:0 range:NSMakeRange(0, string.length)];
if (match.location == NSNotFound) return NO;
else return YES;
于 2013-08-16T13:46:35.880 回答
0

试试这个:

BOOL containsDigits(NSString *s) {
    NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:@"123456789"];
    NSRange range = [s rangeOfCharacterFromSet:cs];
    return range.location != NSNotFound;
}
于 2013-08-16T13:52:53.803 回答
0

通过以下方式修改它

- (BOOL)checkForDigits {
    NSString *editableExpression = self.expression.text;
    BOOL isDigit = FALSE;
    for (int i = 0; i < editableExpression.length; i++){
        char charAtPosition = [editableExpression characterAtIndex:i];
        for (int c = 0; c < 10; c++ ){
            char digitAtPosition = digits[c];
            if (charAtPosition == digitAtPosition){
                 return TRUE;

            }
        }
    }
    return FALSE;
}
于 2013-08-16T13:59:23.817 回答