Runtime 常用API
Runtime API – 类
- 动态创建一个类(参数:父类,类名,额外的内存空间)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
void objc_registerClassPair(Class cls)
void objc_disposeClassPair(Class cls)
Class object_getClass(id obj)
Class object_setClass(id obj, Class cls)
BOOL object_isClass(id obj)
BOOL class_isMetaClass(Class cls)
Class class_getSuperclass(Class cls)
Runtime API02 – 成员变量
Ivar class_getInstanceVariable(Class cls, const char *name)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
void object_setIvar(id obj, Ivar ivar, id value)
id object_getIvar(id obj, Ivar ivar)
- 动态添加成员变量(已经注册的类是不能动态添加成员变量的)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)
const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)
Runtime API03 – 属性
objc_property_t class_getProperty(Class cls, const char *name)
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,
unsigned int attributeCount)
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,
unsigned int attributeCount)
const char *property_getName(objc_property_t property)
const char *property_getAttributes(objc_property_t property)
Runtime API04 – 方法
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)
IMP class_getMethodImplementation(Class cls, SEL name)
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2)
Method *class_copyMethodList(Class cls, unsigned int *outCount)
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
- 获取方法的相关信息(带有copy的需要调用free去释放)
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)
const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)
IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)
Runtime的应用
- Runtime的应用
- 利用关联对象(AssociatedObject)给分类添加属性
- 遍历类的所有成员变量(修改textfield的占位文字颜色、字典转模型、自动归档解档)
- 交换方法实现(交换系统的方法)
- 利用消息转发机制解决方法找不到的异常问题
分类添加属性
@interface Person (Test)
@property (nonatomic,assign) int age;
@end
@implementation Person (Test)
- (void)setAge:(int)age
{
objc_setAssociatedObject(self, @selector(age), @(age), OBJC_ASSOCIATION_ASSIGN);
}
- (int)age
{
return [objc_getAssociatedObject(self, _cmd) intValue];
}
@end
遍历成员变量
@implementation NSObject (Extension)
- (void)getIvars
{
unsigned int outCount
Ivar *ivars = class_copyIvarList([self class], &outCount)
for (int i = 0
Ivar ivar = ivars[i]
NSLog(@"ivar-:%s",ivar_getName(ivar))
}
free(ivars)
}
- 通过给NSObject增加一个分类,添加一个方法,执行方法是会打印出类的所有成员变量。
交换方法
- 通过交换方法实现处理NSMutableArray添加nil对象会崩溃的问题。
#import <objc/runtime.h>
@implementation NSMutableArray (Extension)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class cls = NSClassFromString(@"__NSArrayM");
Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));
Method method2 = class_getInstanceMethod(cls, @selector(xx_insertObject:atIndex:));
method_exchangeImplementations(method1, method2);
});
}
- (void)xx_insertObject:(id)anObject atIndex:(NSUInteger)index
{
if (anObject == nil) return;
[self xx_insertObject:anObject atIndex:index];
}