美文网首页
自定义KVO实现

自定义KVO实现

作者: honzon_0 | 来源:发表于2017-06-27 16:18 被阅读33次

本篇的自定义KVO(不通过中间类)与广为流传的KVO(通过中间类)版本有些许不同,

当然,最主要的是KVO的实现原理,所以代码有点粗糙

自定义类

自定义测试类

@interface Honzon : NSObject
@property (nonatomic, copy)NSString *test;
@end

@interface Honzon ()
@end

@implementation Honzon

- (void)dealloc {
    NSLog(@"Honzon dealloc");
}
@end

自定义KVO信息保存类

key保存属性名称

block保存回调

@interface HZKVOInfo : NSObject
@property (nonatomic, copy)NSString *key;
@property (nonatomic, copy)HZ_KVOBlock block;

- (instancetype)initWithKey:(NSString *)key block:(HZ_KVOBlock)block;
@end
@implementation  HZKVOInfo
- (instancetype)initWithKey:(NSString *)key block:(HZ_KVOBlock)block;{
    self = [super init];
    if (self) {
        _key = key;
        _block = block;
    }
    return self;
}

- (void)dealloc {
    NSLog(@"HZKVOInfo dealloc");
}
@end

自定义其他信息

static NSString *hzKVOSet = @"hzKVOSet";//自定义set方法 前缀
static NSString *kHZKVOAssociatedInfoArrayKey = @"kHZKVOAssociatedInfoArrayKey";//关联key

实现

void hz_KVOImp(id self,SEL _cmd,id newValue) {
    
    //@"setKey:" -> @"key"
    NSString *key = [NSStringFromSelector(_cmd) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];
    key = [key stringByReplacingCharactersInRange:NSMakeRange(key.length - 1, 1) withString:@""];
    NSString *keyStr = [key stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[key substringWithRange:NSMakeRange(0, 1)] lowercaseString]];
    
    //oldValue
    id (*objc_msgSendGet)(id, SEL, id) = (void *)objc_msgSend;
    id oldValue = objc_msgSendGet(self,NSSelectorFromString(keyStr),newValue);
    
    //hzKVOKey SEL
    SEL kvoSEL = NSSelectorFromString([NSString stringWithFormat:@"%@%@:",hzKVOSet,[keyStr capitalizedString]]);
    
    //setKey: -> IMP
    void (*objc_msgSendSet)(id, SEL, id) = (void *)objc_msgSend;
    objc_msgSendSet(self,kvoSEL,newValue);
    
    //关联数组
    NSMutableArray *infoArr = objc_getAssociatedObject(self, (__bridge const void *)(kHZKVOAssociatedInfoArrayKey));
    
    //遍历
    for (HZKVOInfo *info in infoArr) {
        if ([info.key isEqualToString:keyStr]) {
            
            //Block
            info.block(keyStr,oldValue,newValue);
        }
    }
}

-(void)hz_kvoAddObserverForKeyPath:(NSString *)keyPath block:(HZ_KVOBlock)block {
    
    //@"_keyPath"
    NSString *pathName = [NSString stringWithFormat:@"_%@",keyPath];
    
    //setKeyPath SEL
    Ivar keyIvar = class_getInstanceVariable([self class], [pathName UTF8String]);
    SEL keySetter = NSSelectorFromString([NSString stringWithFormat:@"set%@:",[keyPath capitalizedString]]);
    
    //容错
    if (!keyIvar) {  [self doesNotRecognizeSelector:keySetter];}
    
    //关联 数组
    NSMutableArray *infos = objc_getAssociatedObject(self, (__bridge const void *)kHZKVOAssociatedInfoArrayKey);
    if (!infos) {
        infos = [NSMutableArray array];
        objc_setAssociatedObject(self, (__bridge const void *)kHZKVOAssociatedInfoArrayKey, infos, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    // setKeyPath Method
    Method setmethod = class_getInstanceMethod([self class], keySetter);
    
    // hzKVOKeyPath SEL
    SEL kvoSEL = NSSelectorFromString([NSString stringWithFormat:@"%@%@:",hzKVOSet,[keyPath capitalizedString]]);
    
    if (!class_respondsToSelector([self class], kvoSEL)) {
        if(class_addMethod([self class], kvoSEL, (IMP)hz_KVOImp, method_getTypeEncoding(setmethod))) {
            
            //hzKVOKeyPath Method
            Method kvoMehtod = class_getInstanceMethod([self class], kvoSEL);
            
            //交换 IMP
            method_exchangeImplementations(kvoMehtod, setmethod);
            
            //添加 info
            HZKVOInfo *info = [[HZKVOInfo alloc] initWithKey:keyPath block:block];
            [infos addObject:info];
        }
    }
}

按着注释应该很好理解,流程:

  1. 通过名称查找相关key名称的set方法的method
  2. 根据key名称,在类添加相应的kvoSet方法,并取得method
  3. 交换俩个method的实现
  4. 在关联数组里添加信息
  5. 自定义实现hz_KVOImp
  6. 给属性赋值时 setKey(SEL) -> hzKVOSetKey(IMP) -> hzKVOSetKey(SEL) -> setKey(IMP)
  7. 调用block回调

测试

Honzon *h1 = [[Honzon alloc] init];
[h1 hz_kvoAddObserverForKeyPath:@"test" block:^(NSString *keyPath, id oldValue, id newValue) {
    NSLog(@"key:%@  oldValue:%@  newValue:%@",keyPath, oldValue, newValue);
    
    //-> key:test  oldValue:(null)  newValue:test
    //-> key:test  oldValue:test  newValue:test two
}];

h1.test = @"test";
NSLog(@"%@",h1.test);
//-> test

h1.test = @"test two";
NSLog(@"%@",h1.test);
//-> test two



//Honzon dealloc
//HZKVOInfo dealloc

参考博客:如何自己动手实现 KVO

相关文章

  • iOS runtime自定义实现KVO

    1、了解KVO 打印结果: 2、自定义实现KVO .h .m

  • iOS - 自定义KVO

    之前我们已经了解过了KVO的底层实现原理,不过呢,在我们开始实现自定义KVO之前再来简单回顾下KVO的实现原理 1...

  • iOS 自定义KVO

    利用Runtime 实现简单的自定义kvo 代码githubgithub.com/zswj/custom-KVO ...

  • KVO基本使用

    分三部分解释KVO一.KVO基本使用二.KVO原理解析三.自定义实现KVO 一、KVO基本使用 使用KVO,能够非...

  • iOS-底层原理-自定义KVO

    1.自定义KVO 1.上一篇博客了解了iOS 系统KVO的底层实现原理,那么这里进行自定义KVO,更好的理解原理和...

  • KVO 本质 & 自定义实现

    KVO 本质 & 自定义实现 KVO 是什么? Key-Value Observer 即键值观察者。作用为监听某个...

  • 自定义KVO监听方式

    kvo 观察者的本质是,是否有调用set方法 自定义KVO 实现观察People类的name属性 People.h...

  • 自定义KVO实现

    本篇的自定义KVO(不通过中间类)与广为流传的KVO(通过中间类)版本有些许不同, 当然,最主要的是KVO的实现原...

  • UITableView 下拉刷新的实现

    UITableView下拉刷新的实现原理是自定义的下拉刷新控件KVO监听UITableView(UIScrollV...

  • KVO实现原理 自定义KVO

    原理 注册一个中间类KVO_xxx继承自要观察的类,通过isa-swizzling将xxx类的isa指像新的的类K...

网友评论

      本文标题:自定义KVO实现

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