Objective-c – In Objective-C, given an id, how can I tell what type of object it points to

objective-c

Objective-C newbie question. Given the following (fictional) code:

id mysteryObject = [anotherObject mysteriousMethod];

How can I determine at runtime what class mysteryObject is?

Best Solution

You can use isKindOfClass or isMemberOfClass

For example:

if ([foo isMemberOfClass:[NSBar class]])