Study harmoniously! Don’t be impatient!! I’m your old friend, Xiao Qinglong

Class extension – cognition

// main.m

/// class declaration@interface SSJPerson : NSObject @property (nonatomic , strong) NSString *name; @property (nonatomic , assign) NSInteger age; - (void)runInstance; + (void)runClass;
@end

/// class extension@interface SSJPerson () @property (nonatomic , strong)NSString *hoobby_extension; @property (nonatomic , assign)float height_extension; - (void)runInstance_extension; + (void)runClass_extension;
@end

/// Class implementation
@implementation SSJPerson
// This class method- (void)runInstance{}
+(void)runClass{}
// extend the method- (void)runInstance_extension{}
+(void)runClass_extension{}
@end

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

@end
Copy the code

Then in terminal point to the following two commands:

  • $CD Main. m Path of the folder
  • $xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m

This generates a main. CPP file:

Using the.cpp file, we can see that the attributes in the class extension are added to the class structure, and the methods in the class extension are added to the _method_list_t.

Class extension – further exploration

See here, I am a little confused, I do not know if you remember the previous article about classification knowledge, classification properties, methods will eventually load into the class, so class extension is similar to the classification?

With that in mind, we first need to locate when the method is loaded into _method_list_t. Classes are initialized in realizeClassWithoutSwift (), so a breakpoint is set here:

In order to locate the class more quickly and with as little interference as possible, we add a load method to the implementation part of the class

/ / main. M file

/// Class implementation
@implementation SSJPerson
...
+ (void)load{}
...
@end
Copy the code

Operation engineering:

Conclusion:

  • The classified data already exists when the data is read. The data of Data is read from Mach-O.