Compile timeandThe runtime

Compile time is the time when you are compiling. What is compile? The compiler helps you translate the source code into machine-recognized code. The runtime is the code that runs. It’s being loaded into memory.

Runtimeversion

There are two versions of Runtime: a Legacy (earlier version) and a Modern (current version)

  • Earlier versions of the corresponding programming interface:Objective - 1.0 C
  • Current version of the corresponding programming interface:Objective - 2.0 C
  • Earlier versions used forObjective - 1.0 C, 32 bitsMac OS XOn the platform of
  • Current version :iPhone app andMac OS X v10.564-bit programs on and after systems

RuntimeCall in three ways

Framework and Service: for example, isKindofClass.Runtime API: for example, class_getInstanceSize

Method’s underlying implementation

To viewc++The source code

Create a class LKTeacher, and LKPerson inherits from LKTeacher. LKTeacher adds the instance method teacherayHello, LKPerson adds the instance method personSayHello and the class method classSayHello

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        LKPerson *person = [LKPerson alloc];
        [person personSayHello];
        [person teacherayHello];
        [LKPerson classSayHello];
    }
    return 0;
}
Copy the code

Compile the main.m file into the main.cpp file using clang

clang -rewrite-objc main.m -o main.cpp
Copy the code

Look at the main.cpp file

static void _I_LKTeacher_teacherSayHello(LKTeacher * self, SEL _cmd) {
    NSLog((NSString *)&__NSConstantStringImpl__var_folders_07_zf2hbch50bn79j6p806fy1t80000gn_T_main_0a11ef_mi_0);
}

static void _I_LKPerson_personSayHello(LKPerson * self, SEL _cmd) {
    NSLog((NSString *)&__NSConstantStringImpl__var_folders_07_zf2hbch50bn79j6p806fy1t80000gn_T_main_64ea0d_mi_1);
}

static void _C_LKPerson_classSayHello(Class self, SEL _cmd) {
    NSLog((NSString *)&__NSConstantStringImpl__var_folders_07_zf2hbch50bn79j6p806fy1t80000gn_T_main_64ea0d_mi_2);
}

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        LKPerson *person = ((LKPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("LKPerson"), sel_registerName("alloc"));
        ((void (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("personSayHello"));
        ((void (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("teacherSayHello"));
        ((void (*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("LKPerson"), sel_registerName("classSayHello"));
    }
    return 0;
}
Copy the code

As you can see, the methods are implemented using objc_msgSend. In this case, can we call the methods directly by sending a message through objc_msgSend?

objc_msgSend((id)person, sel_registerName("personSayHello"));
Copy the code

#import

Disable objc_msgSend check: Target –> Build Setting –> search objc_msgSend — Enable strict checking of obc_msgSend calls Set to NO

With objc_msgSend and [Person personSayHello] the result is the same. Bottom line: The essence of the method is message sending

A call to a superclass method

As you can see above, [Person teacherayHello] can call a method directly to the parent class. So what’s the flow of calling a superclass method? Convert the lkPerson. m file to the lkPerson.cpp file

((void (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("LKPerson"))}, sel_registerName("teacherSayHello"));
Copy the code

You can see that [super teacherayHello] is implemented with objc_msgSendSuper. You can see that objc_msgSendSuper is the parameter objc_super

struct objc_super {
    /// Specifies an instance of a class.
    __unsafe_unretained _Nonnull id receiver;

    /// Specifies the particular superclass of the instance to message. 
#if! defined(__cplusplus) && ! __OBJC2__/* For compatibility with old objc-runtime.h header */
    __unsafe_unretained _Nonnull Class class;
#else
    __unsafe_unretained _Nonnull Class super_class;
#endif
    /* super_class is the first class to search */
};
Copy the code

The subclass calls the methods of the superclass in objc_msgSendSuper mode, and the essence of the methods is message sending.