Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

For the More Curious: How Does Messaging Work?

As mentioned earlier, an object is like a C struct. NSObject declares an instance variable called isa. Because NSObject is the root of the entire class inheritance tree, every object has an isa pointer to the class structure that created the object (Figure 3.17). The class structure includes the names and types of the instance variables for the class. It also has the implementation of the class’s methods. The class structure has a pointer to the class structure for its superclass.

Figure 3.17.

Figure 3.17. Each Object Has a Pointer to Its Class

The methods are indexed by the selector. The selector is of type SEL. Although SEL is defined to be char *, it is most useful to think of it as an int. Each method name is mapped to a unique int. For example, the method name addObject: might map to the number 12. When you look up methods, you will use the selector, not the string @"addObject:".

As part of the Objective-C data structures, a table maps the names of methods to their selectors. Figure 3.18 shows an example.

Figure 3.18.

Figure 3.18. The Selector Table

At compile time, the compiler looks up the selectors wherever it sees a message send. Thus,

[myObject addObject:yourObject];

becomes (assuming that the selector for addObject: is 12)

objc_msgSend(myObject, 12, yourObject);

Here, objc_msgSend() looks at myObject’s isa pointer to get to its class structure and looks for the method associated with 12. If it does not find the method, it follows the pointer to the superclass. If the superclass does not have a method for 12, it continues searching up the tree. If it reaches the top of the tree without finding a method, the function throws an exception.

Clearly, this is a very dynamic way of handling messages. These class structures can be changed at runtime. In particular, using the NSBundle class makes it relatively easy to add classes and methods to your program while it is running. This very powerful technique has been used to create applications that can be extended by other developers.

  • + Share This
  • 🔖 Save To Your Account