Why is methodLists a pointer to a pointer in objc_class

both method and protocol lists are chaining, protocols are a pointer, but methodLists is a pointer to a pointer, Why?

ocjc_classobjc_method_listobjc_protocol_list

+3


source to share


1 answer


This is an implementation detail, which is a consequence of both Objective-C's history and the ability to dynamically add methods to classes.

In particular, if you look at the contents of the entries in the method_list element, you will find that the methods are broken down into groups, where each collection contains all methods from a specific category of object. That is, if your application were to define a category in a UIView using five methods (don't do it - bad design), then you will find that these five methods are attached to the end of the method_list in a single objc_method_list object (which is why entry_list is pointer aligned and has variable size).



This also applies to adding methods dynamically. There is no need to copy a bunch of data into a newly allocated copy of an existing method data structure. Instead, the runtime can simply assign objc_method_list and stick it at the end of the linked list (or in the chapter for implementation details).

+3


source







All Articles