KVC与KVO简介

 

KVC

-(id)initWithJsonDictionary:(NSDictionary*) jsonDic
{
    if((self = [self init]))
    {
        [self setValuesForKeysWithDictionary:jsonDic];
    }
    return self;
}

-(void)setValue:(id)value forKey:(NSString *)key
{
    if ([key isEqualToString:@"a"]) {
        
    }else{
        [super setValue:value forKey:key];
    }
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    if ([key isEqualToString:@"bb"]) {
        self.b = value;
    }else{
        [super setValue:value forKey:key];
    }
}

KVO

- (void)removeObservation {
    [self.object removeObserver:self
                     forKeyPath:self.property];
}

- (void)addObservation {
    [self.object addObserver:self forKeyPath:self.property
                     options:0
                     context:(__bridge void*)self];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                      context:(void *)context {
  if ((__bridge id)context == self) {
    // 只处理跟我们当前class的property更新
  }
  else {
    [super observeValueForKeyPath:keyPath ofObject:object
                           change:change context:context];
  }
}

 

 

posted on 2014-02-25 11:37  离群的野兽  阅读(202)  评论(0编辑  收藏  举报

导航