1. If you want to know the specific data type in nsnumber, the @ encode keyword will be used.
Use @ encode (Atype) to return the C string of this type, represented by const char. For example, @ encode (INT) returns I; @ encode (float) returns F.
Then, the const char * returned by the objctype method in nsvalue is okay with the comparison above. Use strcmp to compare the size by ASCII value.
int a = 10;NSNumber *number = [NSNumber numberWithInt:a];if (strcmp([number objCType], @encode(int)) == 0) { int result = [number intValue]; NSLog(@"%d", result);}
2. The data type of objective-C, or even the meta type of a custom type, function, or method, can all use ASCII encoding.
NSLog(@"int : %s", @encode(int)); NSLog(@"float : %s", @encode(float)); NSLog(@"float * : %s", @encode(float*)); NSLog(@"char : %s", @encode(char)); NSLog(@"char * : %s", @encode(char *)); NSLog(@"BOOL : %s", @encode(BOOL)); NSLog(@"void : %s", @encode(void)); NSLog(@"void * : %s", @encode(void *)); NSLog(@"NSObject * : %s", @encode(NSObject *)); NSLog(@"NSObject : %s", @encode(NSObject)); NSLog(@"[NSObject] : %s", @encode(typeof([NSObject class]))); NSLog(@"NSError ** : %s", @encode(typeof(NSError **))); int intArray[5] = {1, 2, 3, 4, 5}; NSLog(@"int[] : %s", @encode(typeof(intArray))); float floatArray[3] = {0.1f, 0.2f, 0.3f}; NSLog(@"float[] : %s", @encode(typeof(floatArray))); typedef struct _struct { short a; long long b; unsigned long long c; } Struct; NSLog(@"struct : %s", @encode(typeof(Struct)));
The result is:
int : ifloat : ffloat * : ^fchar : cchar * : *BOOL : cvoid : vvoid * : ^vNSObject * : @NSObject : {NSObject=#}[NSObject] : #NSError ** : ^@int[] : [5i]float[] : [3f]struct : {_struct=sqQ}
Reference: http://nshipster.com/type-encodings/