I hope to check the type of an object, for NSString:
[theObject isKindOfClass:[NSString class]]
it works, but for NSInteger
[theObject isKindOfClass:[NSInteger class]]
will report error
Welcome any comment
Functions to check nil or null object input for NSNumber and NSString. @return If nil input or null object input found, a NSNumber object with zero value will return. If the input is a NSStirng object, a NSNumber object contianing dobleValue of the string will return.
Actually by default in many situations an NSInteger will be nil, because nil is the same as zero... you may well get a compiler warning though. In the sample code given previousScore would be 0 (nil) if [self score] came back nil...
NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char , short int , int , long int , long long int , float , or double or as a BOOL .
The purpose of NSNumber is simply to box primitive types in objects (pointer types), so you can use them in situations that require pointer-type values to work. One common example: you have to use NSNumber if you want to persist numeric values in Core Data entities.
Use the NSNumber class:
if ([obj isKindOfClass:[NSNumber class]]) { ... }
NSNumber Inherits from NSValue : NSObject
NSInteger Used to describe an integer.
#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif
NSInteger
is not an Objective-C class. It's a typedef for an integral type. As such, an object is never going to be a NSInteger.
What you're looking for is the NSNumber
class, which is an Objective-C class.
NSInteger is not an object type. It's a foundation data type. Checkout Foundation Data Types Reference to see how it is defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With