Objective-C @encode关键字
1、如果你想知道NSNumber中具体的数据类型,那么@encode关键字就派上用场了。 使用@encode(aType) 可以返回该类型的C字符串,用const char *表示。比如 @encode(int) 返回的是 i;@encode(float) 返回的是 f。
然后通过NSValue中的 objCType 方法返回的const char *与上面的对比就OK了。使用strcmp按ASCII值大小相比较。
[cpp] view plaincopy
1. int a = 10;
2. NSNumber *number = [NSNumber numberWithInt:a]; 3. if (strcmp([number objCType], @encode(int)) == 0) { 4. int result = [number intValue]; 5. NSLog(@"%d", result); 6.
}
2、Objective-C 的数据类型,甚至自定义类型、函数或方法的元类型,都可以使用 ASCII 编码。
[cpp] view plaincopy
1. NSLog(@"int : %s", @encode(int)); 2. NSLog(@"float : %s", @encode(float)); 3. NSLog(@"float * : %s", @encode(float*)); 4. NSLog(@"char : %s", @encode(char)); 5. NSLog(@"char * : %s", @encode(char *)); 6. NSLog(@"BOOL : %s", @encode(BOOL)); 7. NSLog(@"void : %s", @encode(void)); 8. NSLog(@"void * : %s", @encode(void *)); 9.
10. NSLog(@"NSObject * : %s", @encode(NSObject *)); 11. NSLog(@"NSObject : %s", @encode(NSObject));
12. NSLog(@"[NSObject] : %s", @encode(typeof([NSObject class]))); 13. NSLog(@"NSError ** : %s", @encode(typeof(NSError **))); 14.
15. int intArray[5] = {1, 2, 3, 4, 5};
16. NSLog(@"int[] : %s", @encode(typeof(intArray))); 17.
18. float floatArray[3] = {0.1f, 0.2f, 0.3f};
19. NSLog(@"float[] : %s", @encode(typeof(floatArray))); 20.
21.
typedef struct _struct {
转载自:http://wenku.baidu.com/link?url=v-Ct-eguT6Bd14Iae0427G0m_HR5G4q4-194w2uUAvQDnD7sZSMTwvx4t1GNfi5p9f4ilve-VtsphTEfpQs3bdPa1wESV3Y9A-_Z_G3VN2C
1、如果你想知道NSNumber中具体的数据类型,那么@encode关键字就派上用场了。 使用@encode(aType) 可以返回该类型的C字符串,用const char *表示。比如 @encode(int) 返回的是 i;@encode(float) 返回的是 f。
然后通过NSValue中的 objCType 方法返回的const char *与上面的对比就OK了。使用strcmp按ASCII值大小相比较。
[cpp] view plaincopy
1. int a = 10;
2. NSNumber *number = [NSNumber numberWithInt:a]; 3. if (strcmp([number objCType], @encode(int)) == 0) { 4. int result = [number intValue]; 5. NSLog(@"%d", result); 6.
}
2、Objective-C 的数据类型,甚至自定义类型、函数或方法的元类型,都可以使用 ASCII 编码。
[cpp] view plaincopy
1. NSLog(@"int : %s", @encode(int)); 2. NSLog(@"float : %s", @encode(float)); 3. NSLog(@"float * : %s", @encode(float*)); 4. NSLog(@"char : %s", @encode(char)); 5. NSLog(@"char * : %s", @encode(char *)); 6. NSLog(@"BOOL : %s", @encode(BOOL)); 7. NSLog(@"void : %s", @encode(void)); 8. NSLog(@"void * : %s", @encode(void *)); 9.
10. NSLog(@"NSObject * : %s", @encode(NSObject *)); 11. NSLog(@"NSObject : %s", @encode(NSObject));
12. NSLog(@"[NSObject] : %s", @encode(typeof([NSObject class]))); 13. NSLog(@"NSError ** : %s", @encode(typeof(NSError **))); 14.
15. int intArray[5] = {1, 2, 3, 4, 5};
16. NSLog(@"int[] : %s", @encode(typeof(intArray))); 17.
18. float floatArray[3] = {0.1f, 0.2f, 0.3f};
19. NSLog(@"float[] : %s", @encode(typeof(floatArray))); 20.
21.
typedef struct _struct {
转载自:http://wenku.baidu.com/link?url=v-Ct-eguT6Bd14Iae0427G0m_HR5G4q4-194w2uUAvQDnD7sZSMTwvx4t1GNfi5p9f4ilve-VtsphTEfpQs3bdPa1wESV3Y9A-_Z_G3VN2C