Is there a quick way to use some sort of typeof() equivalent in Objective-C? In Javascript I can type:
var a = "imastring";
console.log(typeof(a));
// --> "string"
I would like something clean in Objective-C that isn't a compiler directive.
Is there a quick way to use some sort of typeof() equivalent in Objective-C? In Javascript I can type:
var a = "imastring";
console.log(typeof(a));
// --> "string"
I would like something clean in Objective-C that isn't a compiler directive.
Yes, you can use the [object class] method:
NSArray *a = @[@"a", @"b"];
NSLog(@"NSArray = %@", [a class]);
// NSArray = __NSArrayI
NSMutableArray *b = [[NSMutableArray alloc] initWithArray:a];
NSLog(@"NSMutableArray = %@", [b class]);
// NSMutableArray = __NSArrayM
NSString *c = @"imastring";
NSLog(@"NSString = %@", [c class]);
// NSString = __NSCFConstantString
NSNumber *d = [[NSNumber alloc] initWithInteger:5];
NSLog(@"NSNumber = %@", [d class]);
// NSNumber = __NSCFNumber
Keep in mind that is is common to see the __NSCF or the __NS prefix on most of the objects you inspect in this manner. For example, when logging the class type of an NSString, you are likely to see __NSCFString or __NSCFConstantString (which are both NSString types) as the result. I only ever use this approach when I am curious to see how things look behind the scenes, but I don't tend to use this type of check in production.
You can look into using the isKindOfClass method to test if the object is a certain type.