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_tThe structure. The structure has aname,cls,List of instance methods,List of class methods,Agreement list,Property list,Class attribute list,The metaclassAnd so on, which is precisely because the classification hasProperty listSo 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.

methodizeClassFunction analysis

frommethodizeClassAnd what we see in the function is that it finally passesattachToClassFunction to theclassificationAdded to theThe main classOn the.

attachToClassfunction

We see theattachToClassFunction codeAnd you can see thatattachToClassfunctionAnd eventually it gets called againattachCategoriesfunction

attachCategoriesfunction

This function handles classification methods, attributes, protocols, and so on.

Classification loading backward path analysis

  • We hit the break point atattachCategoriesIn the functionLGPersonRun the code to the breakpoint location and print the stack informationFrom the figure above we can see that in factClassification load backward pathforattachCategoriyes -> 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 pointreadClsssThe delta function, which goes past usThe association between DYLD and OBJCWe know thisreadClassThe 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 classificationTherefore, 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 classesThe 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 phaseIt’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 phaseLoaded, just inattachToClassThe function is executed twiceload_categories_nolockfunction

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

conclusion