NSString 是整数吗? [英] NSString is integer?
本文介绍了NSString 是整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查 NSString 的内容是否为整数值?有没有现成的方法?
How to check if the content of a NSString is an integer value? Is there any readily available way?
必须有更好的方法来做这样的事情:
There got to be some better way then doing something like this:
- (BOOL)isInteger:(NSString *)toCheck {
if([toCheck intValue] != 0) {
return true;
} else if([toCheck isEqualToString:@"0"]) {
return true;
} else {
return false;
}
}
推荐答案
您可以使用 -intValue
或 -integerValue
方法.如果字符串不是以整数开头,则返回零,这有点遗憾,因为零是整数的有效值.
You could use the -intValue
or -integerValue
methods. Returns zero if the string doesn't start with an integer, which is a bit of a shame as zero is a valid value for an integer.
更好的选择可能是使用 [NSScanner scanInt:]
返回一个 BOOL
指示它是否找到合适的值.
A better option might be to use [NSScanner scanInt:]
which returns a BOOL
indicating whether or not it found a suitable value.
这篇关于NSString 是整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文