I would like to check whether an object (e.g. someObject
) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType
). In Java, I can write:
someObject instanceof SpecifiedType
A related question is finding whether the runtime type of an object is equal to a another type. In Java, I can write:
someObject.getClass().equals(SpecifiedType.class)
How can this be done in Objective-C?
From Wikipedia:
isKindOfClass:
would be closest toinstanceof
, by the sounds of it.See the isKindOfClass: method in the NSObject documentation. (The usual word of warning for such question is that checking the object class is often a sign of doing something wrong.)
Try
[myObject class]
for returning the class of an object.You can make exact comparisons with:
but not by using directly
MyClass
identifier.Similarily, you can find if the object is of a subclass of your class with:
as suggested by Jon Skeet and zoul.