美文网首页
RunTime 之关联对象以及方法互换

RunTime 之关联对象以及方法互换

作者: 点滴86 | 来源:发表于2016-11-30 21:30 被阅读15次

1.关联对象
通过类别为已知类关联对象,与 Associated Objects 相关的函数主要有三个,声明如下:

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
id objc_getAssociatedObject(id object, const void *key)
void objc_removeAssociatedObjects(id object)

这三个函数的作用如下:
objc_setAssociatedObject 用于给对象添加关联对象,传入nil 则可以移除已有的关联对象。
objc_getAssociatedObject 用于获取关联对象。
objc_removeAssociatedObjects 用于移除一个对象的所有关联对象。
示例如下

UIViewController+AssociatedObjects.h 代码

#import <UIKit/UIKit.h>

@interface UIViewController (AssociatedObjects)

- (NSString*)author;

- (void)setAuthor:(NSString*)name;

@end

UIViewController+AssociatedObjects.m 代码

#import "UIViewController+AssociatedObjects.h"
#import <objc/runtime.h>

static char UIViewControllerAssociatedObjectAuthorKey;

@implementation UIViewController (AssociatedObjects)

- (NSString*)author
{
    NSString *tempAuthor = @"";
    tempAuthor = objc_getAssociatedObject(self, &UIViewControllerAssociatedObjectAuthorKey);
    
    return tempAuthor;
}

- (void)setAuthor:(NSString *)name
{
    objc_setAssociatedObject(self, &UIViewControllerAssociatedObjectAuthorKey, name, OBJC_ASSOCIATION_COPY);
}

@end

使用如下:

self.author = @"DianDi86";
 NSLog(@"%@",self.author);

console log 如下

logOne.png

2.方法互换
通过method_exchangeImplementations 实现方法互换.
Vehicle.h

#import <Foundation/Foundation.h>

@interface Vehicle : NSObject

- (void)testMethodSwizzling;

@end

Vehicle.m

#import "Vehicle.h"

@implementation Vehicle

- (void)testMethodSwizzling
{
    NSLog(@"Vehicle testMethodSwizzling Start");
    
    NSLog(@"Vehicle testMethodSwizzling End");
}

@end

Car.h

#import "Vehicle.h"

@interface Car : Vehicle

@end

Car.m

#import "Car.h"

@implementation Car

- (void)testMethodSwizzling
{
    NSLog(@"Car testMethodSwizzling Start");
    
    [super testMethodSwizzling];
    
    NSLog(@"Car testMethodSwizzling End");
}

@end

Vehicle+Replacement.h

#import "Vehicle.h"

@interface Vehicle (Replacement)

@end

Vehicle+Replacement.m

#import "Vehicle+Replacement.h"
#import <objc/runtime.h>

@implementation Vehicle (Replacement)

+ (void)load
{
    Method testMethodSwizzling_ = class_getInstanceMethod([self class], @selector(testMethodSwizzling));
    Method replacementTestMethodSwizzling_ = class_getInstanceMethod([self class], @selector(replacementTestMethodSwizzling));
    
    if (testMethodSwizzling_ && replacementTestMethodSwizzling_ && strcmp(method_getTypeEncoding(testMethodSwizzling_), method_getTypeEncoding(replacementTestMethodSwizzling_)) == 0) {
        method_exchangeImplementations(testMethodSwizzling_, replacementTestMethodSwizzling_);
    }
}

- (void)replacementTestMethodSwizzling
{
    NSLog(@"replacementTestMethodSwizzling Start");
    
    [self replacementTestMethodSwizzling];
    
    NSLog(@"replacementTestMethodSwizzling End");
}

@end

使用如下

Car *audiA4L = [[Car alloc] init];
    [audiA4L testMethodSwizzling];

console log 如下

logTwo.png

测试界面如下

运行时功能.png

相关文章

  • RunTime 之关联对象以及方法互换

    1.关联对象通过类别为已知类关联对象,与 Associated Objects 相关的函数主要有三个,声明如下: ...

  • 关联对象底层结构

    关联对象的方法 runtime给我们提供了三个关于关联对象的方法,如下。点击查看如何添加关联对象[https://...

  • [iOS开发]--Runtime的简单使用之关联对象

    一、Runtime关联对象的方法简介: 在中,有三个关联的方法,分别是: 1.1、...

  • Runtime 三:runtime 中常用的 API 介绍

    当目前为止我们已经了解了runtime 是如何查找方法,以及如何缓存方法; 如何关联对象,给分类添加成员变量;Cl...

  • runtime 之关联对象

    如何给一个NSArray添加一个属性,不能使用继承。 分类不能添加属性,只能添加方法。这时就可以使用关联对象。 关...

  • Runtime 之关联对象

    关联对象是指某个OC对象通过一个唯一的key连接到一个类的实例上。 关联对象的原理: Runtime提供的API:...

  • iOS 系统源码及第三方源码总结

    1.系统源码总结 RunTime源码阅读(一)之weakRunTime源码阅读(二)关联对象RunTime源码阅读...

  • Object-C关联对象

    什么是关联对象 关联对象是指某个对象通过唯一的key连接到一个类的实例上 runtime提供的方法 举例说明 关联...

  • runtime之关联对象及方法交换

    关联对象 runtime的对象关联,如果有了解的话就会知道,他是通过一个key,将两个对象或者对象和某个类之间进行...

  • 底层面试分析

    【面试-1】Runtime Asssociate方法关联的对象,需要在dealloc中释放? 当我们对象释放时,会...

网友评论

      本文标题:RunTime 之关联对象以及方法互换

      本文链接:https://www.haomeiwen.com/subject/yubtmttx.html