determine if a string is a number with NSScanner

夙愿已清 提交于 2019-12-24 08:48:14

问题


i'm trying to find out if my string contains any floatValue, and resign the first responder if it's the case, if it's not, the textfield keyboard should stay on screen.

This code always hides the keyboard, even if it's not a floatValue : do you know how to make it work?

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSScanner *scan = [NSScanner scannerWithString:[textField text]];
    if ([scan scanFloat:NULL]){

        [password resignFirstResponder];
        [passwordLength resignFirstResponder];
        return YES;

    } else {
        return NO;
    }
}

Also, i haven't tried with loops but this is a beginning, if you have any idea :

BOOL doesStringContain(NSString* string, NSString* string2){
    for (int i=0; i<[string length]; i++) {
        NSString* chr = [string substringWithRange:NSMakeRange(i, 1)];
        for (int j=0; j<[string2 length]; j++){
            if([chr isEqualToString:j])
                return TRUE;
        }
    }
    return FALSE;
}

Thanks a lot


回答1:


NSScanner will expect that your float be at the beginning of your string. So to combat that we use setCharactersToBeStripped.

setCharactersToBeStripped will filter out all non-numeric non-period characters from the string so that all you're left with to scan is the number that you're looking for.

NSScanner will match int values (without the .) as well as it will equate an int 123 to 123.00000.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    
    NSScanner *scan = [NSScanner scannerWithString:[textField text]];
    [scan setCharactersToBeSkipped:[[NSCharacterSet characterSetWithCharactersInString:@"1234567890."] invertedSet]];
    float f;
    if ([scan scanFloat:&f]){
        NSLog(@"Scanned a float %f",f);
        [textField resignFirstResponder];
        return YES;

    } else {
        NSLog(@"Did not scan a float");
        return NO;
    }
}

If you want to check if there are non-numeric characters vs only numerics then try :

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    

    NSCharacterSet *withFloats = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];

    NSCharacterSet *withoutFloats = [NSCharacterSet decimalDigitCharacterSet];

    // Change withFloats <-> withoutFloats depending on your need
    NSString *newString = [[textField.text componentsSeparatedByCharactersInSet:withFloats] componentsJoinedByString:@""];

    NSLog(@"newString %@", newString);

    if ([newString isEqualToString:@""]){
        NSLog(@"Scanned a numeric");
        [textField resignFirstResponder];
        return YES;

    } else {
        NSLog(@"Did not scan a numeric");
        return NO;
    }
}



回答2:


You are implementing the wrong delegate method. textFieldShouldReturn: is called when the user hits the return key. (Not when control is trying to "return from" the field, in case that's what you were thinking.)

You should implement textFieldShouldEndEditing: instead. That is where you can (try to) stop the text field from losing first responder status, and keep the keyboard up. Just check the text like you are doing, and return YES or NO (let the system update first responder status).

If you want the return key to dismiss the keyboard if input is valid, you should call endEditing: there. Like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField endEditing:NO];
}

The NO parameter means don't force it, basically allowing your textFieldShouldEndEditing: code to check the text first. (You should always call endEditing: if you can, rather than resignFirstResponder.)

Note that, for various reasons, the text field might be forced to give up first responder status anyway, so even if you're validating input in this way, be prepared to validate it again before you save it to disk or send it over the network or whatever you want to do with it.



来源:https://stackoverflow.com/questions/7349316/determine-if-a-string-is-a-number-with-nsscanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!