Jul 07

Objective C is a loosely typed language, where type “id” is passed to most methods. When operating with an “id” you can call methods (message) that may not even exist. This will create a run-time exception, not a compile time error. Moving from a strongly typed language like Java, this is a little disconcerting. I’ve been making sure to learn all about Objective C’s Type Introspection in order to combat my fear.

There are two functions for Type Introspection: isKindOfClass and isMemberOfClass.

If we have the following class structure:

ClassRelationship

The following conditions apply:

[dog isKindOfClass:[Animal class]]; //is TRUE

[dog isMemberOfClass:[Dog class]]; //is TRUE

[dog isMemberOfClass:[Animal class]]; //is FALSE

So, isKindOfClass denotes whether a class is an instance of another class or an instance of a subclass. While isMemberOfClass denotes if the class is an instance of the specified class (i.e. subclasses do not evaluate to true).

For Java folks, isKindOfClass is equivalent to instanceof and isMemberOfClass is equivalent to (obj.getClass() == MyObject.class).

Comments are closed.

preload preload preload