2012年4月1日 星期日

[Objective C] How to use the instance method - objCType

Below just a note of my studying

NSNumber * n = [NSNumber numberWithBool:YES];
if (strcmp([n objCType], @encode(BOOL)) == 0) {
    NSLog(@"this is a bool");
} else if (strcmp([n objCType], @encode(int)) == 0) {
    NSLog(@"this is an int");
}
objcType will return a char or a string to represent the datatype of the method receiver.
this representation can be derived by using @encode() to a specific datatype.
to get the representation char/string of that data type.


and strcmp() compare these two strings, and return a boolean value, which shows whether the date type of "n" is equal to BOOL or not.






沒有留言:

張貼留言