Question

In Apple's ObjC Runtime Guide, it describes what the objc_msgSend() function does for dynamic dispatch:

  1. It first finds the procedure (method implementation) that the selector refers to. Since the same method can be implemented differently by separate classes, the precise procedure that it finds depends on the class of the receiver.
  2. It then calls the procedure, passing it the receiving object (a pointer to its data), along with any arguments that were specified for the method.
  3. Finally, it passes on the return value of the procedure as its own return value.

I was confused in the second step, where it mentioned "receiving object (a pointer to its data)

What is that?

Can somebody give me a illustration to clarify it?

Was it helpful?

Solution

This will explain it in detail: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/

In short, every Objective-C method is really a C function that has two mandatory arguments and then whatever arguments are passed to the method.

I.e. this:

- (void) foo:sender;

- (void) foo:(id)sender;

Is really this C function:

void foo(id self, SEL _cmd, id sender);

The pointer to the data refers to the self parameter. Through that pointer to an object, the compiler generates all references to any instance variables of self.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top