我试着搜索过,但是我觉得我的谷歌搜索技巧不够好,因此我向人们寻求帮助。我正在扫描一个字符串并取出其中的每个元素。我需要找出它是数字还是文本。我是这样进行检查的:
// 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
}
然而,我发现Objective-C不会抛出异常,它只会返回0。我不能依赖于用户不使用零作为输入的事实,我该怎么办?
提前感谢您,我相信有类似的讨论,我只是没有找到...
NSScanner
是最好的方法来实现这一点。请参见@VinceBurn的答案。 - nielsbotNSString doubleValue
不会抛出异常。这不是Java。 - rmaddy