NSString是整数? [英] NSString is integer?

查看:160
本文介绍了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 方法。如果字符串不是以一个羞耻的整数开头,则返回0 ...零是整数的有效值...

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...

更好的选项可能要使用 > > > 无论它是否找到合适的值。

A better option might be to use [NSScanner scanInt:] which returns a BOOL indicating whether or not it found a suitable value.

这篇关于NSString是整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆