Detailed explanation of objective c messaging mechanism

  • 2020-04-01 21:31:32
  • OfStack

There are two ways of passing methods in objective-c: Selector Blocks. This article is mainly about selectors, and we will summarize them in the following sections about Blocks.

The Message Passing model is the core mechanism of objective-c. In objective-c, there's no such thing as method calls, just messaging.

Calling a method on a class in C++ or Java is sending a message to that class in objective-c. In C++ or Java, the relationship between class and class behavior methods is so tight that a method must belong to a class and be bound together at compile time, so you can't call a method that isn't in the class. In objective-c, however, it's simpler, where the class is loosely coupled to the message, and the method call simply sends a message to a class that can decide at runtime what to do with the received message. That is to say, a class does not guarantee that will respond to messages received, if received a message unable to process, the program will not go wrong or goes down, it is just do nothing, and returns a nil (the author add: it is not wrong at compile time, conform to the semantic understanding, but the runtime runtime, crash 】 . The design itself also fits the software metaphor. (very nice. I saw it on the Internet. Here it is.)

Obviously, since the compiler doesn't have a location method, there's only a runtime location method, so how does objective-c go about locating the location at runtime?

Id objc_msgSend(id receiver, SEL selector,...) Contains two required parameters: receiver(receiver object), selector(method selector), and an unknown parameter (selector parameter list).

Objective-c looks up the calling method through the above method ~ like [itNoob cry] is converted to objc_msgSend(itNoob,cry), where the receiver is the itNoob object, the selector is the cry selector, and of course, if cry has an argument, it's also converted, Such as [itNoob cry:@" vuvuzia "AndSmile:@" hee hee "] will be converted to objc_msgSend(itNoob,cry:AndSmile:,@" vuvuzia ",@" hee hee "), similar to objc_msgSend(id receiver, SEL selector, parm1,parm2...) .

Dynamic binding procedure for objc_msgSend

Calls the found implementation according to the receiver object to look up the specific implementation of the selector method, passes the argument and returns the return value of the method implementation as its own

Objc_msgSend is how to find the specific implementation of the method, from the Internet, as follows:

When the compiler builds each class, each class must contain two necessary elements:

Pointer to the parent class a dispatch table that associates the selector of the class with the actual memory address of the method.

We know that every object has an isa pointer to its class, and this isa pointer can be used to find the class to which the object belongs and the parent class to which the object belongs...

The search process is as follows :

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201211/2012112117091193.png ">

When sending a message to an object, find the class it belongs to according to isa, then look for the dispatch table of that class, if not, go to its parent class. If it is found, the implementation is called based on the memory address in the scheduling table, and returns nil if it is never found.


Related articles: