gpt4 book ai didi

objective-c - 验证文本字段中的数字(0-9)

转载 作者:行者123 更新时间:2023-12-01 18:31:03 25 4
gpt4 key购买 nike

我正在尝试仅验证数字的文本字段。

我正在使用以下方法:

if(isnumber(noOfPassengers.text.intValue))
{
NSLog(@"value entered correctly.");
return ;
}
else
NSLog(@"Error: Only numerica values are accepted");

isnumber()函数无法正常工作。我只想验证数字。卡在这里。还有其他内置功能可以检查数字甚至字符吗?

最佳答案

使用NSScanner:

int iValue;

if (noOfPassengers.text.length > 0 && [[NSScanner scannerWithString:noOfPassengers.text] scanInt:&iValue]) {
//do smomething with iValue (int value from noOfPassengers.text)
NSLog(@"value entered correctly.");
return ;
}
else
NSLog(@"Error: Only numerica values are accepted");

关于objective-c - 验证文本字段中的数字(0-9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com