runtime学习

//判断类中是否包含某个方法的实现
BOOL class_respondsToSelector(Class cls, SEL sel)
//获取类中的方法列表
Method *class_copyMethodList(Class cls, unsigned int *outCount)
//为类添加新的方法,如果方法该方法已存在则返回NO
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
//替换类中已有方法的实现,如果该方法不存在添加该方法
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
//获取类中的某个实例方法(减号方法)
Method class_getInstanceMethod(Class cls, SEL name)
//获取类中的某个类方法(加号方法)
Method class_getClassMethod(Class cls, SEL name)
//获取类中的方法实现
IMP class_getMethodImplementation(Class cls, SEL name)
//获取类中的方法的实现,该方法的返回值类型为struct
IMP class_getMethodImplementation_stret(Class cls, SEL name)

//获取Method中的SEL
SEL method_getName(Method m)
//获取Method中的IMP
IMP method_getImplementation(Method m)
//获取方法的Type字符串(包含参数类型和返回值类型)
const char *method_getTypeEncoding(Method m)
//获取参数个数
unsigned int method_getNumberOfArguments(Method m)
//获取返回值类型字符串
char *method_copyReturnType(Method m)
//获取方法中第n个参数的Type
char *method_copyArgumentType(Method m, unsigned int index)
//获取Method的描述
struct objc_method_description *method_getDescription(Method m)
//设置Method的IMP
IMP method_setImplementation(Method m, IMP imp)
//替换Method
void method_exchangeImplementations(Method m1, Method m2)

//获取SEL的名称
const char *sel_getName(SEL sel)
//注册一个SEL
SEL sel_registerName(const char *str)
//判断两个SEL对象是否相同
BOOL sel_isEqual(SEL lhs, SEL rhs)

//通过块创建函数指针,block的形式为^ReturnType(id self,参数,…)
IMP imp_implementationWithBlock(id block)
//获取IMP中的block
id imp_getBlock(IMP anImp)
//移出IMP中的block
BOOL imp_removeBlock(IMP anImp)

//调用target对象的sel方法
id objc_msgSend(id target, SEL sel, 参数列表…)

  • (void)addAction:(ButtonBlock)block
    {
    objc_setAssociatedObject(self, &ActionTag, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    }

  • (void)action:(id)sender
    {
    ButtonBlock blockAction = (ButtonBlock)objc_getAssociatedObject(self, &ActionTag);
    if (blockAction)
    {
    blockAction(self);
    }
    }

  • (BOOL)resolveInstanceMethod:(SEL)sel {
    NSLog(@" resolve instance method: %@", NSStringFromSelector(sel));
    BOOL resolved = [super resolveInstanceMethod:sel];
    if (!resolved) {
    // 动态添加一个方法_dynamic_method_imp_处理消息
    class_addMethod([self class], sel, (IMP)dynamic_method_imp, “v@:”);
    return YES; // 返回YES,表示消息转发成功,不会发生崩溃
    }
    return resolved;
    }
  • (id)forwardingTargetForSelector:(SEL)aSelector {
    NSLog(@“forwarding target for selector: %@”, NSStringFromSelector(aSelector));
    id cls = [super forwardingTargetForSelector:aSelector];
    if (cls == nil) {
    // 使用代理类处理消息
    ForwardProxy *p = [[ForwardProxy alloc] init];
    if ([p respondsToSelector:aSelector]) {
    return p; // 返回非nil,非self的对象,表示消息转发成功,不会发生崩溃
    }
    }
    return cls;
    }

//方法中返回一个方法签名

  • (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSMethodSignature *ms = [super methodSignatureForSelector:aSelector];
    if (ms == nil) {
    // 创建一个非nil的方法签名,否则,不会进入forwardInvocation:方法进行消息转发
    ms = [ForwardProxy instanceMethodSignatureForSelector:@selector(missMethod)];
    }
    return ms;
    }
    //发给每个需要实现这个方法的对象

  • (void)forwardInvocation:(NSInvocation *)anInvocation {
    if (anInvocation) {
    // 处理转发的消息,进入此方法,就不会产生崩溃
    [self missTarget:[anInvocation target] withSelector:[anInvocation selector]];
    }
    }

  • (id)transformToObject:(NSObject *)object
    {
    self.object = object;
    return self.object;
    }

  • (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
    {
    NSMethodSignature *methodSignature;
    if (self.object) {
    methodSignature = [self.object methodSignatureForSelector:sel];
    }else{
    methodSignature = [super methodSignatureForSelector:sel];
    }
    return methodSignature;
    }

  • (void)forwardInvocation:(NSInvocation *)invocation
    {
    if (self.object) {
    [invocation setTarget:self.object];
    [invocation invoke];
    }
    }

block未实现其方法就访问
当遇到了EXC_BAD_ACCESS异常,意味着访问了一个已经被释放的内存区域。

load 方法调用时机,而且只调用当前类本身,不会调用superClass 的 +load 方法:

class_ro_t存放的是编译期间就确定的;而class_rw_t是在runtime时才确定,它会先将class_ro_t的内容拷贝过去,然后再将当前类的分类的这些属性、方法等拷贝到其中。所以可以说class_rw_t是class_ro_t的超集,当然实际访问类的方法、属性等也都是访问的class_rw_t中的内容

分类原理就是在运行时通过runtime的方法将所有分类方法加入到一个全局数组,然后找到类对象的methodlist属性,将原来的类对象的方法往后挪动n个指针,再把n个分类方法加入到前面

运行runtime的realizeClass 方法时,会生成class_rw_t结构体,该结构体包含了class_ro_t,并且更新data部分,换成class_rw_t结构体的地址

//创建继承自NSObject类的People类
Class People = objc_allocateClassPair([NSObject class], “People”, 0);
//将People类注册到runtime中
objc_registerClassPair(People);
//注册test: 方法选择器
SEL sel = sel_registerName(“test:”);
//函数实现
IMP imp = imp_implementationWithBlock(^(id this,id args,…){
NSLog(@“方法的调用者为 %@”,this);
NSLog(@“参数为 %@”,args);
return @“返回值测试”;
});

//向People类中添加 test:方法;函数签名为@@😡,
// 第一个@表示返回值类型为id,
// 第二个@表示的是函数的调用者类型,
// 第三个:表示 SEL
// 第四个@表示需要一个id类型的参数
class_addMethod(People, sel, imp, “@@😡”);
//替换People从NSObject类中继承而来的description方法
class_replaceMethod(People,@selector(description), imp_implementationWithBlock(^NSString*(id this,…){
return @“我是Person类的对象”;}),
“@@:”);

//完成 [[People alloc]init];
id p1 = objc_msgSend(objc_msgSend(People, @selector(alloc)),@selector(init));
//调用p1的sel选择器的方法,并传递@"???“作为参数
id result = objc_msgSend(p1, sel,@”???");
//输出sel方法的返回值
NSLog(@“sel 方法的返回值为 : %@”,result);

//获取People类中实现的方法列表
NSLog(@“输出People类中实现的方法列表”);
unsigned int methodCount;
Method * methods = class_copyMethodList(People, &methodCount);
for (int i = 0; i<methodCount; i++) {
NSLog(@“方法名称:%s”,sel_getName(method_getName(methods[i])));
NSLog(@“方法Types:%s”,method_getDescription(methods[i])->types);
}
free(methods);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值