Advanced iOS: Objective-C runtime (a)

  The first time I saw runtime, feel too big, the dynamic access methods, properties, etc. simply do not do the powerful. After + practice to find information, find runtime and not as complex, followed by a basic overview of the runtime.

  To use the runtime methods need to be introduced runtime.h file

  First, the basics  

  Method: member method

  Ivar: member variables

  Second, the common method

  class_copyPropertyList: get list of properties

  class_copyMethodList: get a list of member methods

  class_copyIvarList: get variable list members

  ivar_getName: get the variable name

  property_getName: get the property name

 

  Example of use:

  1. Get a list of member variables

 

// 1. Get Variable List 
        unsigned int ivarCount = 0 ; // number of variables members 
        Ivar = class_copyIvarList ivarList * ([Self class ], & ivarCount); // Ivar array 
        
        for ( int I = 0 ; I <ivarCount; I ++) { // traverse 
            Ivar Ivar = ivarList [i]; // get Ivar 
            const  char * name = ivar_getName (Ivar); // get the variable name 
            NSString * Key = [NSString stringWithUTF8String: name]; 
            NSLog ( @ " % @ " , Key );

        }

      free(ivarList);
 

  2. Get the list of attributes

 

unsigned int COUNT = 0 ; 
    objc_property_t * propertList = class_copyPropertyList ([Self class ], & COUNT);
     for ( int I = 0 ; I <COUNT; I ++ ) { 
        objc_property_t Property = propertList [I];
         const  char * name = property_getName ( Property);
         const  char * attrs = property_getAttributes (Property);
 //         property_copyAttributeValue (,) is the first parameter objc_property_t, the second parameter "V" acquisition variable name, "T" Get type 
        const  char *value = property_copyAttributeValue(property, "V");
        NSLog(@"name = %s, attrs = %s, value = %s", name, attrs, value);
    }

    free(propertList);

  3. Get a list of methods 

 

unsigned int count = 0;
    Method *methodList = class_copyMethodList([self class], &count);
    for (int i = 0 ; i < count; i++) {
        Method method = methodList[i];
        SEL selector = method_getName(method);//方法入口
        const char *sel_name = sel_getName(selector);
        NSLog(@"方法名 %s", sel_name);
    }
    free(methodList);

 

  Third, the use directions: archiving, Dictionary <----> model, like frame package

  Achieve Archive

  

#define WKCodingImplementing \
- (void)encodeWithCoder:(NSCoder *)aCoder \
{ \
    unsigned int ivarCount = 0; \
    Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \
    for (int i = 0; i < ivarCount; i++) { \
        Ivar ivar = ivarList[i]; \
        const char *name = ivar_getName(ivar); \
        const char *type = ivar_getTypeEncoding(ivar); \
        NSLog(@"%s-----%s", name, type); \
        NSString *key = [NSString stringWithUTF8String:name]; \
        id value = [self valueForKey:key]; \
        [aCoder encodeObject:value forKey:key]; \
    } \
    free(ivarList); \
} \
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder \
{ \
    if (self = [super init]) { \
        unsigned int ivarCount = 0; \
        Ivar *ivarList = class_copyIvarList([self class], &ivarCount); \
        for (int i = 0; i < ivarCount; i++) { \
            Ivar ivar = ivarList[i]; \
            const char *name = ivar_getName(ivar); \
            NSString *key = [NSString stringWithUTF8String:name]; \
            NSLog(@"%@ %@", key, value); \
            id value = [aDecoder decodeObjectForKey:key]; \
            [self setValue:value forKey:key]; \
        } \
    } \
    return self; \
}

 

 

 

  

Reproduced in: https: //www.cnblogs.com/pretty-guy/p/4860265.html

Guess you like

Origin blog.csdn.net/weixin_34064653/article/details/93438723