1 introduction

For some mysterious reason, we did some simple tests on the loading mechanism of multiple dynamic libraries.

Ps. Some of the results may overturn many netizens’ perceptions

2+loadMethod execution timingmayPredating the class itself+loadmethods

First, let’s prepare the two dynamic libraries separately and add the following test code

Dynamic library A:

Dynamic library B:

The corresponding logs are as follows:

Note: The +load method of the ClassA class executes earlier than the +load method of ClassA itself

Cause analysis:

  1. Upon startup, DYLD reads the dynamic libraries that the APP depends on in turn

  2. The load_images function of libobjc.a.dylib is told to perform the load task in reverse order

  3. Execute the load methods in the following order:

    1. The class itself+loadmethods
    2. Classification of+loadmethods
  4. C++ static initialization methods and functions modified by __attribute__((constructor))

In this case, FrameWorkB is the last dynamic library to be relied upon, so the internal +load method of The ClassA class of FrameWorkB executes before the +load method of ClassA itself

2. Class instance methods override class instance methods

Dynamic library A:

Dynamic library B:

The main program:

From the log, we can see that the test method of the classification must override the test method of the class itself

Recommended at the end of the article: iOS popular anthology

  • 1.BAT and other major manufacturers iOS interview questions + answers

  • 2.Must-read books for Advanced Development in iOS (Classics must Read)

  • 3.IOS Development Advanced Interview “resume creation” guide

  • (4)IOS Interview Process to the basics