Basics of Introspection for iOS Apps - dummies

Basics of Introspection for iOS Apps

By Rajiv Ramnath

In iOS app programming, introspection refers to facilities in a programming language that examine the structure and other details of an object and its class at runtime. Such details can include:

  • An object’s inheritance tree

  • Whether it conforms to a specific protocol

  • Whether it responds to a certain message

When you know what an object is made of, you can programmatically construct messages to send to it, which makes your program much more dynamic. This would also be the time to determine the class of an object for programming. You can define the class of an object at runtime. In fact, Objective-C treats classes as first-class objects.

Every class object points to a data structure in memory, has a superclass (known as a metaclass), and can respond to messages that invoke methods.

When you call a class method, you’re really sending a message to the object of the class.

You can get a reference to the class object for the class by sending the class a class message, and you get the superclass of a class by sending the object a superclass message. For example, you can print the class and the superclass of an object, such as a StackOfInteger object, as follows:

StackOfInteger *aStack = [[StackOfInteger alloc] init];
NSLog(@"Class is %@, and super is %@.",[aStack class],[aStack superclass]);