简体   繁体   English

如何确定目标C中输入的是数字还是文本?

[英]How to find out whether input is number or text in objective-c?

I've tried to search, but I think my google is skill is not good enough, so I'm asking a help from people. 我尝试搜索,但是我认为我的Google技能还不够好,所以我要求人们提供帮助。 I'm scanning through a string and taking each element. 我正在扫描一个字符串并获取每个元素。 I need to find out whether it's number or text. 我需要找出它是数字还是文本。 I did my checking in that way: 我以这种方式进行检查:

// Try to convert to double
@try{
double whatever=[myString doubleValue];

// If text is stored in myString, then it should throw an exception
} @catch (NSException *e){

// Do whatever I need, but here I know that it's text, not a number


}

However I found out that Objective-C doesn't throw an exception, it just returns 0. I can't rely on the fact that user won't use zero as an input, what should I do? 但是我发现Objective-C不会引发异常,它只会返回0。我不能依靠用户不会将零用作输入这一事实,我该怎么办?

Thanks in advance and I'm pretty sure there are treads like this, I just didn't find them... 在此先感谢,我很确定这里有这样的花纹,我只是没有找到它们...

Convert to double and back to string. 转换为双精度并返回字符串。 If it matches the source, then it was a double. 如果它与源匹配,则为两倍。 Careful with leading and trailing spaces. 注意前导和尾随空格。

Try this: 尝试这个:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber *number = [formatter numberFromString:myString];

if (number) {
  // it's a number
} else {
  // it's not a number
}

You can use the class NSScanner to find out if you got a valid double with this method : 您可以使用NSScanner类使用以下方法找出是否有有效的double

- (BOOL)scanDouble:(double *)doubleValue

once you have initialized your scanner with your string. 使用字符串初始化扫描仪后。


Why (double *) ? 为什么(double *)
Because you want the method to return to you 2 informations, the doubleValue and is it a valid double . 因为您希望该方法返回2条信息, the doubleValue is it a valid double The latter is given by the BOOL and for the former only a Pointer can get you a value out of a method call (apart from the return value) 后者由BOOL给出,对于前者,只有一个Pointer可以从方法调用中获取值(除了返回值之外)

Parameters 参量
doubleValue doubleValue
Upon return, contains the scanned value. 返回时,包含扫描值。 Contains HUGE_VAL or –HUGE_VAL on overflow, or 0.0 on underflow. 上溢时包含HUGE_VAL或–HUGE_VAL,下溢时包含0.0。
Return Value 返回值
YES if the receiver finds a valid floating-point representation, otherwise NO. 如果接收器找到有效的浮点表示,则为“是”,否则为“否”。

NSMutableString *stringElement = [NSMutableString stringWithCapacity:[yourString length]];
for (int i=0; i<[yourString length]; i++) {
    if (isdigit([stringElement characterAtIndex:i])) {
      //is digit
    }
}

Example: 例:

NSError* error;
NSString* string=@"10";
NSNumberFormatter* formatter=[NSNumberFormatter new];
formatter.numberStyle= NSNumberFormatterDecimalStyle; // set whatever style you need
NSNumber* number;
if([formatter getObjectValue: &number forString: string errorDescription: &error])
{
    // You got the number
    NSLog(@"%@",number);
}
else
{
    // haven't got the number, the string is invalid
    NSLog(@"%@",error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM