美文网首页
高性能iOS应用开发 笔记08

高性能iOS应用开发 笔记08

作者: 梓华 | 来源:发表于2018-09-04 17:59 被阅读5次
    Jietu20180904-170843.jpg
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.person = [[Person alloc] init];
        self.person.name = @"Amy";
        
        __weak typeof(self) weakSelf = self;
        
        self.resultLabelMonitor = [ObserverManager addObserverToObject:self.person forKey:@"name" block:^(NSDictionary *changes) {
            typeof(weakSelf) sself = weakSelf;
            
            if(sself)
            {
                sself.resultLabel.text = [changes valueForKey:@"new"];
            }
        }];
        
        self.person.name = @"Mary";
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    - (IBAction)onClickClose:(id)sender
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (void)dealloc
    {
        NSLog(@"Page5VC dealloc");
    }
    
    
    @interface Person : NSObject
    
    @property (nonatomic, copy) NSString *name;
    
    @end
    
    @interface ObserverManager : NSObject
    
    + (ObserverObserveeHandle *)addObserverToObject:(NSObject *)obj forKey:(NSString *)keyPath block:(void (^)(NSDictionary *))inputBlock;
    
    @end
    
    + (ObserverObserveeHandle *)addObserverToObject:(NSObject *)obj forKey:(NSString *)keyPath block:(void (^)(NSDictionary *))inputBlock
    {
        MyObserver *observer = [[MyObserver alloc] init];
        observer.block = inputBlock;
        
        [obj addObserver:observer forKeyPath:keyPath options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
        
        ObserverObserveeHandle *details = [[ObserverObserveeHandle alloc] initWithObserver:observer target:obj keyPath:keyPath];
        
        return details;
    }
    
    @interface MyObserver : NSObject
    
    @property (nonatomic, copy) void (^block)(NSDictionary *);
    
    @end
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
    {
        NSLog(@"%@对象的%@属性改变了:%@", object, keyPath, change);
        
        if(self.block)
        {
            self.block(change);
        }
    }
    
    - (void)dealloc
    {
        NSLog(@"MyObserver dealloc");
    }
    
    @interface ObserverObserveeHandle : NSObject
    
    @property (nonatomic, strong) MyObserver *observer;
    @property (nonatomic, strong) NSObject *obj;
    @property (nonatomic, copy) NSString *keyPath;
    
    - (id)initWithObserver:(MyObserver *)observer target:(NSObject *)obj keyPath:(NSString *)keyPath;
    
    @end
    
    - (id)initWithObserver:(MyObserver *)observer target:(NSObject *)obj keyPath:(NSString *)keyPath
    {
        self = [super init];
        
        if(self)
        {
            self.observer = observer;
            self.obj = obj;
            self.keyPath = keyPath;
        }
        
        return self;
    }
    
    - (void)removeObserver
    {
        [self.obj removeObserver:self.observer forKeyPath:self.keyPath context:nil];
        
        self.observer = nil;
        
        self.obj = nil;
    }
    
    - (void)dealloc
    {
        NSLog(@"ObserverObserveeHandle dealloc");
        
        [self removeObserver];
    }
    

    相关文章

      网友评论

          本文标题:高性能iOS应用开发 笔记08

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