5.常见方法

unsigned int count;

获取属性列表

objc_property_t *propertyList = class_copyPropertyList([self class], &count);

for (unsigned int i=0; i%@", [NSString stringWithUTF8String:propertyName]);

}

获取方法列表

Method *methodList = class_copyMethodList([self class], &count);
for (unsigned int i; i%@", NSStringFromSelector(method_getName(method)));

}

获取成员变量列表

Ivar *ivarList = class_copyIvarList([self class], &count);
  for (unsigned int i; i%@", [NSString stringWithUTF8String:ivarName]);

  }

获取协议列表

__unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);

  for (unsigned int i; i%@", [NSString stringWithUTF8String:protocolName]);
  }

现在有一个Person类,和person创建的xiaoming对象,有test1和test2两个方法

获得类方法

Class PersonClass = object_getClass([Person class]);
SEL oriSEL = @selector(test1);
Method oriMethod = class_getInstanceMethod(xiaomingClass, oriSEL);

获得实例方法

Class PersonClass = object_getClass([xiaoming class]);
SEL oriSEL = @selector(test2);
Method cusMethod = class_getInstanceMethod(xiaomingClass, oriSEL);

添加方法

BOOL addSucc = class_addMethod(xiaomingClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));

替换原方法实现

class_replaceMethod(toolClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));

交换两个方法

method_exchangeImplementations(oriMethod, cusMethod);