使用 NSScanner 确定字符串是否为数字

发布于 2024-12-03 11:25:39 字数 870 浏览 3 评论 0原文

我试图找出我的字符串是否包含任何 floatValue,如果是的话,请辞去第一响应者的职务,如果不是,则文本字段键盘应保留在屏幕上。

此代码始终隐藏键盘,即使它不是 floatValue :您知道如何使其工作吗?

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

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

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

    } else {
        return NO;
    }
}

另外,我还没有尝试过循环,但这是一个开始,如果您有任何想法:

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;
}

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-12-10 11:25:39

NSScanner 会期望您的浮点数位于字符串的开头。因此,为了解决这个问题,我们使用 setCharactersToBeStripped。

setCharactersToBeStripped 将从字符串中过滤掉所有非数字非句点字符,以便您剩下要扫描的就是您要查找的数字。

NSScanner 将匹配 int 值(不带 .),并将 int 123 等同于 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;
    }
}

如果您想检查是否存在非数字字符与仅数字字符,请尝试:

- (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;
    }
}

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;
    }
}
不顾 2024-12-10 11:25:39

您正在实施错误的委托方法。 textFieldShouldReturn: 当用户按下返回键时调用。 (不是当控件试图“从”字段“返回”时,以防万一这就是您的想法。)

您应该改为实现 textFieldShouldEndEditing: 。您可以(尝试)阻止文本字段失去第一响应者状态,并保持键盘处于打开状态。只需像您正在做的那样检查文本,然后返回 YES 或 NO(让系统更新第一响应者状态)。

如果您希望在输入有效的情况下返回键关闭键盘,您应该在那里调用 endEditing: 。像这样:

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

NO 参数意味着不要强制它,基本上允许您的 textFieldShouldEndEditing: 代码首先检查文本。 (如果可以的话,您应该始终调用 endEditing:,而不是 resignFirstResponder。)

请注意,由于各种原因,文本字段可能会被迫放弃第一响应者状态,因此,即使您以这种方式验证输入,在将其保存到磁盘或通过网络发送或执行任何您想要的操作之前,请准备好再次验证它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文