iOS underlying learning-day-16

Keywords: JSON iOS Programming Attribute

iOS underlying learning-day-16

Preface - OC grammar

I am an iOS developer. I am a rookie at the bottom of iOS for 30 days.

problem

What is Runtime? Has it ever been used in normal projects?

  • OC is a dynamic programming language, which allows many operations to be delayed until the program runs
  • The dynamics of OC is supported and implemented by Runtime, which is a set of C language API and encapsulates many dynamic related functions
  • Normally, the OC code written at the bottom is converted into Runtime API for calling

Specific application of Runtime

  • Using association object to add attribute to classification
  • Traverse all the member variables of the class (modify textfield's placeholder text color, dictionary conversion model, and auto archive and de file)
  • Implementation of switching method (method of switching system)
  • Using the mechanism of message forwarding to solve the problem of exception that can not be found

runtime API-01 class

  • Get Class pointed by isa
Class object_getClass(id obj)
  • Set the Class pointed by isa
Class object_setClass(id obj, Class cls)
object_setClass(person,@selector(run));
  • Judge whether an OC object is a Class
BOOL object_isClass(id obj)
NSLog(@"%d %d %d",
          object_isClass(person),//Object? Isclass to see if it is a class object
          object_isClass([MJPerson class]),
          object_isClass(object_getClass([MJPerson class]))
          );
  • Create a class dynamically (parameters: parent class, class name, extra memory space) / / member variables cannot be added dynamically because they are read-only
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
  • Add member variables dynamically (registered classes cannot add member variables dynamically)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)
  • Example 1
	void run(id self, SEL _cmd) {
    	NSLog(@"_____ %@ - %@", self, NSStringFromSelector(_cmd));
	}
	// Create class
    Class newClass = objc_allocateClassPair([NSObject class], "MJDog", 0);
    class_addIvar(newClass, "_age", 4, 1, @encode(int));
    class_addIvar(newClass, "_weight", 4, 1, @encode(int));
    class_addMethod(newClass, @selector(run), (IMP)run, "v@:");
    // Registration class
    objc_registerClassPair(newClass);

//    MJPerson *person = [[MJPerson alloc] init];
//    object_setClass(person, newClass);
//    [person run]; / / this is the time to run to newClass
    
    id dog = [[newClass alloc] init];
    [dog setValue:@10 forKey:@"_age"];
    [dog setValue:@20 forKey:@"_weight"];
    [dog run];
    
    NSLog(@"%@ %@", [dog valueForKey:@"_age"], [dog valueForKey:@"_weight"]);
    
    // Release when this class is not needed
    objc_disposeClassPair(newClass); //Release class
  • Set and get the value of a member variable
void object_setIvar(id obj, Ivar ivar, id value)
id object_getIvar(id obj, Ivar ivar)
  • Get an instance variable information
Ivar class_getInstanceVariable(Class cls, const char *name)
  • Copy the instance variable list (free release needs to be called finally)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
  • Get information about member variables
const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)
  • Example 2
 	// Get member variable information
    Ivar ageIvar = class_getInstanceVariable([MJPerson class], "_age");
    NSLog(@"%s %s", ivar_getName(ageIvar), ivar_getTypeEncoding(ageIvar));

    // Set and get the value of a member variable
    Ivar nameIvar = class_getInstanceVariable([MJPerson class], "_name");
    
    MJPerson *person = [[MJPerson alloc] init];
    object_setIvar(person, nameIvar, @"123");
    object_setIvar(person, ageIvar, (__bridge id)(void *)10);
    NSLog(@"%@ %d", person.name, person.age);

    // Number of member variables
    unsigned int count;
    Ivar *ivars = class_copyIvarList([MJPerson class], &count);
    for (int i = 0; i < count; i++) {
        // Take out the member variable of i position
        Ivar ivar = ivars[I];//*[ivars + i]
        NSLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
    }
    free(ivars);

Dictionary to model example

  • NSObject+Json.h
#import <Foundation/Foundation.h>

@interface NSObject (Json)

+ (instancetype)mj_objectWithJson:(NSDictionary *)json;

@end
  • NSObject+Json.m
#import "NSObject+Json.h"
#import <objc/runtime.h>

@implementation NSObject (Json)

+ (instancetype)mj_objectWithJson:(NSDictionary *)json {
    id obj = [[self alloc] init];
    
    unsigned int count;
    Ivar *ivars = class_copyIvarList(self, &count);
    for (int i = 0; i < count; i++) {
        // Take out the member variable of i position
        Ivar ivar = ivars[i];
        NSMutableString *name = [NSMutableString stringWithUTF8String:ivar_getName(ivar)];
        [name deleteCharactersInRange:NSMakeRange(0, 1)];
        
        // Set value
        id value = json[name];
        if ([name isEqualToString:@"ID"]) {
            value = json[@"id"];
        }
        [obj setValue:value forKey:name];
    }
    free(ivars);
    
    return obj;
}

@end

runtime API-04 method replacement

  • Copy method list (free release needs to be called finally)
Method *class_copyMethodList(Class cls, unsigned int *outCount)
  • Dynamic add method
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
  • Get the information about the method (if you have copy, you need to call free to release it)
SEL method_getName(Method m)
IMP method_getImplementation(Method m)
const char *method_getTypeEncoding(Method m)
unsigned int method_getNumberOfArguments(Method m)
char *method_copyReturnType(Method m)
char *method_copyArgumentType(Method m, unsigned int index)
  • Get an instance method and a class method
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)
  • Method implementation is important
IMP class_getMethodImplementation(Class cls, SEL name) 
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2) 
  • method_exchangeImplementations
MJPerson *person = [[MJPerson alloc] init];
        
Method runMethod = class_getInstanceMethod([MJPerson class], @selector(run));
Method testMethod = class_getInstanceMethod([MJPerson class], @selector(test));
method_exchangeImplementations(runMethod, testMethod);

[person run];//It becomes test
  • Dynamic replacement is important
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
  • class_replaceMethod
MJPerson *person = [[MJPerson alloc] init];
//class_replaceMethod([MJPerson class], @selector(run), (IMP)myrun, "v");
    
class_replaceMethod([MJPerson class], @selector(run), imp_implementationWithBlock(^{
     NSLog(@"123123");
}), "v");
    
[person run];
Published 17 original articles, praised 0, visited 705
Private letter follow

Posted by mjs87 on Tue, 14 Jan 2020 20:46:54 -0800