In Objective-C, how do I test the object type?

James Skidmore picture James Skidmore · Jul 17, 2009 · Viewed 247.8k times · Source

I need to test whether the object is of type NSString or UIImageView. How can I accomplish this? Is there some type of "isoftype" method?

Answer

mmc picture mmc · Jul 17, 2009

If your object is myObject, and you want to test to see if it is an NSString, the code would be:

[myObject isKindOfClass:[NSString class]]

Likewise, if you wanted to test myObject for a UIImageView:

[myObject isKindOfClass:[UIImageView class]]