Runtime二

一、分类

  • 1、给分类添加属性
@interface MyObject (Runtime)@property (nonatomic, copy) NSString * name; @end

#import @implementation MyObject (Runtime)- (void)setName:(NSString *)name{ objc_setAssociatedObject(self, @"name", name, OBJC_ASSOCIATION_COPY); } - (NSString *)name{ return objc_getAssociatedObject(self, @"name"); } @end

  • 2、释放关联的对象
@interface MyObject (Runtime)@property (nonatomic, strong) MyTestObject * testObject; @end

@implementation MyObject (Runtime) - (void)setTestObject:(MyTestObject *)testObject{ objc_setAssociatedObject(self, @"testObject", testObject, OBJC_ASSOCIATION_RETAIN); } - (MyTestObject *)testObject{ return objc_getAssociatedObject(self, @"testObject"); } @end

- (void)viewDidLoad { [super viewDidLoad]; _obj = [[MyObject alloc] init]; _obj.testObject = [MyTestObject new]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ _obj = nil; } 运行结果 :testObject对象被释放。

二、交换方法
  • __unsafe_unretained__weak
    __unsafe_unretained: 不会对对象进行retain,当对象销毁时,会依然指向之前的内存空间(野指针)
    __weak: 不会对对象进行retain,当对象销毁时,会自动指向nil
Person *p = [[Person alloc] init]; __unsafe_unretained Person *p1 = p; p = nil; //p指向nil,p1的弱引用指针不会销毁,会继续指向对象的地址,对象已经销毁,此时p1访问的是"僵尸"对象 NSLog(@"%@",p1); 访问对象已经销毁 -[Person respondsToSelector:]: message sent to deallocated instance 0x100302560

  • 交换方法
+ (void)load{ [self exchangeMethod]; } + (void)exchangeMethod{ SEL originSEL = @selector(testMethod:); //定一个函数指针(因为imp【函数指针】没有传参数的方式) __block void (*originIMP)(id, SEL, NSString*) = NULL; // 将要用来替换原有的函数实现 id newBlock = ^(id self, NSString *str){ NSLog(@"newBlock::%@", str); // 执行旧的方法 if (originIMP) { originIMP(self, _cmd, str); } }; // 把block转化成 IMP IMP newIMP = imp_implementationWithBlock(newBlock); Method originMethod = class_getInstanceMethod([self class], originSEL); // 替换原有的函数指针,返回原有的函数指针originIMP originIMP =(__typeof__(originIMP))method_setImplementation(originMethod, newIMP); if(originIMP){ NSLog(@"method_setImplementation 成功"); } } - (void)testMethod:(NSString*)str{ NSLog(@"testMethod::%@", str); }

三、消息转发
- (void)viewDidLoad { [super viewDidLoad]; void (*msg)(id,SEL) = (__typeof__(msg))objc_msgSend; msg(self,@selector(testMessage)); } - (void)testMessage{ NSLog(@"%s",__func__); }

- (void)viewDidLoad { [super viewDidLoad]; NSString * (*msg)(id,SEL,NSString *) = (__typeof__(msg))objc_msgSend; NSString * str = msg(self,@selector(testMessage:),@"ads"); NSLog(@"%@",str); } - (NSString *)testMessage:(NSString *)str{ return str; }

四、总结 runtime是如何建立起在OC的? 【Runtime二】oc: messaegSend
runtime: objc_msgSend(self, SEL, ...)
执行的时候 IMP(通过发送着,SEL,去找实现)
五、面向动态性。 最终实现是没有在编译的时候确定,在执行的时候,通过查找来确定(编译的时候函数地址不确定,运行时根据SEL查找确定)
结构:
消息机制:1、修改方法 2、获取隐藏变量,通过KVC直接修改
六、类方法为什么不能添加属性。 类中的结构体已经确定,不能动态添加属性。

    推荐阅读