Runtime gets a list of object properties and methods

Create a category of NSObject and introduce runtime

#import <objc/runtime.h>

.m file


/**
 * Get all attributes and their corresponding values
 *
 */
- (NSDictionary *) getAllPropertiesAndValues ​​{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount;
    // linked list of properties
    objc_property_t *properties =class_copyPropertyList([self class], &outCount);
    //traverse the linked list
    for (int i = 0; i<outCount; i++)
    {
        objc_property_t property = properties[i];
        // get the attribute string
        const char* propertyName =property_getName(property);
        //Convert to NSString
        NSString *key = [NSString stringWithUTF8String:propertyName];
        //Get the value corresponding to the attribute
        id value = [self valueForKey:key];
        if (value)
        {
            [props setObject:value forKey:key];
        }
    }
    // release the structure array memory
    free(properties);
    return props;
}

/**
 * Get all properties of the object
 *
 * @return attribute array
 */
- (NSArray *) getAllProperties
{
    unsigned int count;
    //Get the linked list of properties
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    
    for (int i = 0; i < count ; i++)
    {
        objc_property_t property = properties[i];
        const char* propertyName =property_getName(property);
        [propertiesArray addObject: [NSString stringWithUTF8String:propertyName]];
    }
    
    free(properties);
    
    return propertiesArray;
}

/**
 * Get all methods of the object
 */
-(NSArray *)getAllMethods
{
    unsigned int count_f =0;
    // get method list
    Method* methodList_f = class_copyMethodList([self class],&count_f);
    
    NSMutableArray *methodsArray = [NSMutableArray arrayWithCapacity:count_f];
    
    for(int i=0;i<count_f;i++)
    {
        Method temp_f = methodList_f[i];
        // method call address
        IMP imp_f = method_getImplementation(temp_f);
        //method
        SEL name_f = method_getName(temp_f);
        // method name string
        const char* name_s =sel_getName(method_getName(temp_f));
        //number of parameters
        int arguments = method_getNumberOfArguments(temp_f);
        // Returns a string describing the method's parameters and return value
        const char* encoding =method_getTypeEncoding(temp_f);
        NSLog(@"Method name: %@, Number of parameters: %d, Encoding method: %@",[NSString stringWithUTF8String:name_s],
              arguments,[NSString stringWithUTF8String:encoding]);
        
        NSString *methodStr = NSStringFromSelector(name_f);
        [methodsArray addObject:methodStr];
        
    }
    free(methodList_f);
    
    return methodsArray;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474968&siteId=291194637