iOS runtime获取所以方法列表和成员变量


#import "NSObject+runtime.h"
#import 

@implementation NSObject (runtime)
/* 获取对象的所有属性 */
+(NSArray *)getAllProperties {
    u_int count;
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    
    for (int i = 0; i < count ; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
    
    NSLog(@"====> %@ : %@", NSStringFromClass([self class]), propertiesArray.description);
    return propertiesArray;
}



/* 获取对象的所有方法 */
+(NSArray *)getAllMethods {
    unsigned int methodCount =0;
    Method* methodList = class_copyMethodList([self class],&methodCount);
    NSMutableArray *methodsArray = [NSMutableArray arrayWithCapacity:methodCount];
    
    for(int i=0;i %@ : %@", NSStringFromClass([self class]), methodsArray.description);
    return methodsArray;
}


/* 获取对象的所有属性和属性内容 */
+ (NSDictionary *)getAllPropertiesAndVaules:(NSObject *)obj {
    NSMutableDictionary *propsDic = [NSMutableDictionary dictionary];
    unsigned int outCount;
    objc_property_t *properties =class_copyPropertyList([obj class], &outCount);
    for ( int i = 0; i %@ : %@", NSStringFromClass([self class]), propsDic.description);
    return propsDic;
}

@end

 

你可能感兴趣的:(iOS)