KVC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
-( 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]; } }
原文:http://www.cnblogs.com/senlinwuran/p/3566342.html