Objective c: Check if integer/int/number

前端 未结 6 2127
不知归路
不知归路 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:08

    You can use the intValue method on NSString:

    NSString *myString = @"123";
    [myString intValue]; // returns (int)123
    

    Here is the Apple documentation for it - it will return 0 if it's not a valid integer: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue

    Hope that helps!

提交回复
热议问题