preface
We explored class loading in our last article on loading basics of iOS. At the end of the article we also looked at the code that handles classes. This time we’ll look at class loading.
Nature of classification
To study categorization loading, let’s first look at the nature of categorization.
Explore the nature of classification through source code
We searched the source codecategory_t
, the search results are as followsFrom the above we can conclude that the essence of classification iscategory_t
The structure. The structure has aname
,cls
,List of instance methods
,List of class methods
,Agreement list
,Property list
,Class attribute list
,The metaclass
And so on, which is precisely because the classification hasProperty list
So categories can be added dynamicallyattribute
(Classification can only declare attributes, because there are no set and GET methods, so you need to add them dynamically
).
Classification loading
We’ll use LGPerson as the main class, and we’ll add LGPerson+LGA and LGPerson+LGB. We learned from the class loading in the previous article that the information of the class will eventually be associated with the realizeClassWithoutSwift function, and the realizeClassWithoutSwift function will handle the classification through methodizeClass function.
methodizeClass
Function analysis
frommethodizeClass
And what we see in the function is that it finally passesattachToClass
Function to theclassification
Added to theThe main class
On the.
attachToClass
function
We see theattachToClass
Function codeAnd you can see thatattachToClass
functionAnd eventually it gets called againattachCategories
function
attachCategories
function
This function handles classification methods, attributes, protocols, and so on.
Classification loading backward path analysis
- We hit the break point at
attachCategories
In the functionLGPerson
Run the code to the breakpoint location and print the stack informationFrom the figure above we can see that in factClassification load backward path
forattachCategoriyes
->load_categories_nolock
(Execute twice) ->loadAllCategories
->load_images
Summary of forward and backward paths for classification loading
Main class classification mixed loading
The main difference between the main class and the class mixed loading is whether the lazy loading class, that is, whether the +load function is implemented
Lazy loading classes mixed with lazy loading classes
We set a breakpoint in the main function, as shownWe find that we are only executing at this pointreadClsss
The delta function, which goes past usThe association between DYLD and OBJC
We know thisreadClass
The function is executed at compile time, so you know that it is not loaded at this pointClass and its classification
. So let’s go ahead and execute the code to NSLog, and nowWe found it loadedClass and its classification
Therefore, we can conclude that:Lazy-loaded classes are mixed with lazy-loaded classes, and loading is actually delayed until the first message is forwarded, which is [LGPerson alloc] in this article
Non-lazy-loaded classes are mixed with lazy-loaded classes
Still the sameLazy loading classes mixed with lazy loading classes
The analysis is the same,We can see from the diagram that there is also execution[LGPerson alloc]
, the class has already been loaded, i.eCompilation phase
It’s already loaded
Non-lazy-loaded classes are mixed with non-lazy-loaded classes
We know from the picture above that it is also inCompilation phase
Loaded, just inattachToClass
The function is executed twiceload_categories_nolock
function
Lazy loading classes mixed with lazy loading classes
From the picture aboveMixing lazy-loaded classes with lazy-loaded classes forces the main class to load at compile time