And*_*son 6 iphone cocoa-touch objective-c
以下代码将标识字符串是否为整数 - 即字符串仅包含数字.但是,我讨厌这段代码.什么是更好的方法?
NSString *mightBeAnInteger = fooString;
int intValue = [fooString intValue];
if (intValue > 0
&& [[NSString stringWithFormat:@"%d",intValue] isEqualToString:mightBeAnInteger]) {
NSLog(@"mightBeAnInteger is an integer");
}
Run Code Online (Sandbox Code Playgroud)
kev*_*boh 21
[fooString rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location == NSNotFound 如果字符串中只包含数字字符,则为YES.
请注意,这不会捕获负数,因此您必须添加负号(可能通过抓取mutableCopy并添加符号).
| 归档时间: |
|
| 查看次数: |
8400 次 |
| 最近记录: |