Objective c: Check if integer/int/number

前端 未结 6 2126
不知归路
不知归路 2021-02-13 02:25

In objective c, how can i check if a string/NSNumber is an integer or int

6条回答
  •  梦如初夏
    2021-02-13 03:10

    if( [(NSString *)someString intValue] )
    { /* Contains an int fosho! */ }
    
    if( [(NSNumber *)someNumber intValue] )
    { /* Contains an int wich is not 0.... :D */ }
    

    You can ofcourse first determine whether its a NSString or NSNumber by using

    [Object isKindOfClass:[NSString class]] etc...

    – boolValue – charValue – decimalValue – doubleValue – floatValue – intValue – integerValue – longLongValue – longValue – shortValue – unsignedCharValue – unsignedIntegerValue – unsignedIntValue – unsignedLongLongValue – unsignedLongValue – unsignedShortValue

    are all methods of NSNumber to get its values. NSString got a few similar ones.

提交回复
热议问题