KVC/KVO
第wangzz 第http://blog.csdn.net/wzzvictory/article/details/9674431 到⽣生增⼿手wangzzstrive 增等
第 1你KVC/KVO增看真以 ⾏行 2KVO增对KVC增KVC ⾏行
KVC/KVO中种开就增Cocoa微NSObject NSKeyValueCoding/NSKeyValueObserving就样就 ⾏行样增KVC/KVO 主⾏行 增KVC/KVOObjective-C⼤大runtime增 只⾏行 ⼼心增KVC/KVO⽽而家增学⾏行
1KVC
Key-value coding增全之得⾏行增时map
经活⾏行⼒力觉⽽而家⽽而两 ⾏行
2KVO
Key-value observing增全之得中种⾏行
还的⾏行MVCCocoa增KVO
⾃自modelcontroller⾏行
KVC
1KeyKey Path
KVC⽽而增⽽而第 [java] view plaincopy
1. #$(id)valueForKey:(NSString$*)key;$$
2. #$(void)setValue:(id)value$forKey:(NSString$*)key;$$
3. #$(id)valueForKeyPath:(NSString$*)keyPath;$$
4. #$(void)setValue:(id)value$forKeyPath:(NSString$*)keyPath;$$
Key增⽽而⼒力觉⾏行 KeyPath给⼒力动⽽而在 ⼒力觉情⾏行KeyPath address.street⽽而是⽆无 address⽆无street⾏行KeyPath 给⽽而时⼈人⼒力觉⾏行
2KVC
⽽而家增来KVC⽽而增 ⾏行⽽而增来增KVC ⾏行明第KVC⽽而得⾏行 3To-Many 明To-One,Person name增⾏行增Person friendsName增⼦子明Objective-CNSArray增NSSet 增说⾏行 给增第
给
有KVC⼦子增⼦子给⼦子⾏行
给
开发增后⼀一就
⽽而⼦子不⾏行
情⼦子第
[java] view plaincopy
-
#countOf<Key>$$
-
//NSArraycount:$$
-
#objectIn<Key>AtIndex:$$
-
#<key>AtIndexes:$$
-
//$NSArray$
$objectAtIndex:$$objectsAtIndexes:$$
-
#get<Key>:range:$$
-
//$NSArray$ $getObjects:range:$$
-
$$
-
#insertObject:in<Key>AtIndex:$$
-
#insert<Key>:atIndexes:$$
-
//$NSMutableArray$
$insertObject:atIndex:$$insertObjects:atIndexes:$$
-
#removeObjectFrom<Key>AtIndex:$$
-
#remove<Key>AtIndexes:$$
-
//$NSMutableArray$
$removeObjectAtIndex:$$removeObjectsAtIndexes:$$
-
#replaceObjectIn<Key>AtIndex:withObject:$$
-
#replace<Key>AtIndexes:with<Key>:$$
-
//$$
情⼦子第
[java] view plaincopy
-
#countOf<Key>$$
-
//NSArraycount:$$
-
#objectIn<Key>AtIndex:$$
-
#<key>AtIndexes:$$
-
//$NSArray$
$objectAtIndex:$$objectsAtIndexes:$$
-
#get<Key>:range:$$
-
//$NSArray$ $getObjects:range:$$
-
$$
-
#insertObject:in<Key>AtIndex:$$
-
#insert<Key>:atIndexes:$$
-
//$NSMutableArray$
$insertObject:atIndex:$$insertObjects:atIndexes:$$
-
#removeObjectFrom<Key>AtIndex:$$
-
#remove<Key>AtIndexes:$$
-
//$NSMutableArray$
$removeObjectAtIndex:$$removeObjectsAtIndexes:$$
-
#replaceObjectIn<Key>AtIndex:withObject:$$
-
#replace<Key>AtIndexes:with<Key>:$$
-
//$$
增增 ⽤用都增⾏行得在增友 个就样就增在我⾏行 4Key-Value Validation KVC们KeyValue第
[java] view plaincopy
1. #$(BOOL)validateValue:(inout$id$*)ioValue$forKey:
(NSString$*)inKey$error:(out$NSError$**)outError;$$
很⼀一就第
[java] view plaincopy
1. #$(BOOL)validate<Key>:error:$$
name第 [java] view plaincopy
-
#(BOOL)validateName:(id$*)ioValue$error: (NSError$*$__autoreleasing$*)outError${$$
-
$$$$//$Implementation$specific$code.$$
-
$$$$return$...;$$
-
}$$
然⾏行
在增KVC之得们增们
⾏行真以增CoreData⾏行
5KVC
得如着增次⾏行
KVC⾏行KVC得如
着NSNumberNSValue增⾃自不⾏行
两增PersonNSIntegerage
还的得
KVC真以就想age得第 [java] view plaincopy
1. [person$setValue:[NSNumber$numberWithInteger:5]$forKey:@"age"];$$
ageNSNumber增KVCNSNumber也 NSInteger增⽽而家想age得⾏行
得 增就age得第
[java] view plaincopy
1. [person$valueForKey:@"age"];$$
增NSNumber就age得⾏行
增NSNumber增NSValue意
间NSNumber
NSNumber着第 [java] view plaincopy
-
+$(NSNumber$*)numberWithChar:(char)value;$$
-
+$(NSNumber$*)numberWithUnsignedChar:(unsigned$char)value;$$
-
+$(NSNumber$*)numberWithShort:(short)value;$$
-
+$(NSNumber$*)numberWithUnsignedShort:(unsigned$short)value;$$
-
+$(NSNumber$*)numberWithInt:(int)value;$$
-
+$(NSNumber$*)numberWithUnsignedInt:(unsigned$int)value;$$
-
+$(NSNumber$*)numberWithLong:(long)value;$$
-
+$(NSNumber$*)numberWithUnsignedLong:(unsigned$long)value;$$
-
+$(NSNumber$*)numberWithLongLong:(long$long)value;$$
-
+$(NSNumber$*)numberWithUnsignedLongLong:(unsigned$long$long)value;$$
-
+$(NSNumber$*)numberWithFloat:(float)value;$$
-
+$(NSNumber$*)numberWithDouble:(double)value;$$
-
+$(NSNumber$*)numberWithBool:(BOOL)value;$$
-
+$(NSNumber$*)numberWithInteger:
(NSInteger)value$NS_AVAILABLE(10_5,$2_0);$$
-
+$(NSNumber$*)numberWithUnsignedInteger:
(NSUInteger)value$NS_AVAILABLE(10_5,$2_0);$$
得着⾏行 NSValue
NSValue如着增⼦子如第 [java] view plaincopy
-
+$(NSValue$*)valueWithCGPoint:(CGPoint)point;$$
-
+$(NSValue$*)valueWithCGSize:(CGSize)size;$$
-
+$(NSValue$*)valueWithCGRect:(CGRect)rect;$$
-
+$(NSValue$*)valueWithCGAffineTransform:
(CGAffineTransform)transform;$$
-
+$(NSValue$*)valueWithUIEdgeInsets:(UIEdgeInsets)insets;$$
-
+$(NSValue$*)valueWithUIOffset:
(UIOffset)insets$NS_AVAILABLE_IOS(5_0);$$
6等如会意增如 NSValue增了去第 http://blog.csdn.net/wzzvictory/article/details/8614433 6Collection Operators
⼦子⼒力被Key Path增了果valueForKeyPath第 增增valueForKey:情话⽅方⾏行 ⼒力@被⼒力觉增⼀一就要第
么⼦子⼒力
么⼦子⼒力@avg增@count增@max增@min增@sum5增要
增不⾏行
⼦子第transactions增说Transaction
两增第payee增amount增date⾏行两
⼒力第
amount什得第 [java] view plaincopy
1. NSNumber$*transactionAverage$=$[transactions$valueForKeyPath:@"@avg .amount"];$$
transactions⼦子不第 [java] view plaincopy
1. NSNumber$*numberOfTransactions$=$[transactions$valueForKeyPath:@"@c ount"];$$
增@count⼦子⼒力被增
增⾏行
⼒力
⼦子⼒力能点增和就在增第
[java] view plaincopy
1. @distinctUnionOfObjects$$
2. @unionOfObjects$$ 得NSArray增新三增
⽂文⼦子⾏行 第
[java] view plaincopy
1. NSArray$*payees$=$[transactions$valueForKeyPath:@"@distinctUnionOfO bjects.payee"];$$
2. NSArray$*payees$=$[transactions$valueForKeyPath:@"@unionOfObjects.p ayee"];$$
好机能增好机 ⾏行
间ArraySet给⼒力
能点增⼦子⽆无⼦子增现上
第
[java] view plaincopy
1. //$Create$the$array$that$contains$additional$arrays.$$
2. self.arrayOfTransactionsArray$=$[NSMutableArray$array];$$
3. $$$
4. //$Add$the$array$of$objects$used$in$the$above$examples.$$
5. [arrayOfTransactionsArray$addObject:transactions];$$
6. $$$
-
//$Add$a$second$array$of$objects;$this$array$contains$alternate$val ues.$$
-
[arrayOfTransactionsArrays$addObject:moreTransactions];$$
⽆无⼦子⼦子第arrayOfTransactionsArray 给arrayOfTransactionsArray⽆无⼦子增
⼒力第
[java] view plaincopy
1. @distinctUnionOfArrays$$
2. @unionOfArrays$$
3. @distinctUnionOfSets$$
⼦子Arrays增⼦子Sets⾏行Sets 三增@unionOfSets⼒力⾏行
两第
[java] view plaincopy
1. NSArray$*payees$=$[arrayOfTransactionsArrays$valueForKeyPath:@"@uni
onOfArrays.payee"];$$
1KVC KVC时⽽而家⾏行⽽而家 增增KVC为⽽而家到⽣生⾏行想 增KVC出情真以第 做这说-<key>-is<key>明得-get<key>⽽而 家增增得⽂文 做这说-set<key>:增想得⾏行-get<key>- set<key>:增⼈人Key⼒力觉增Cocoa 吧⽂文 只增做这-_<key>-_is<key>明得 -_get<key>-_set<key>:⽂文 间⽽而家增⽽而两⾏行两 第<key>_<key>; 增valueForUndefinedKey:setValue:forUndefinedKey: ⾏行很增着⼈人⾏行 2KVC/KVO 之得之得中种着isa-swizzling真以增着runtime ⼤大⾏行上成去第 http://blog.csdn.net/kesalin/article/details/8194240 时中种增⼤大知 增知⼈人中种 setter ⾏行 知⼈人 setter 增之得中 种⾏行想 setter 增⼈人 KVO ⾏行主每 KVO 想就 得增那还的增 KVO ⾏行 知⼈人 class “更”地⾏行 isa 在在才知增
知增 setter ⼈人 setter增 没之得⾏行增知⼈人 dealloc 学看微⾏行 ⼈人增学两增⾏行 去Objective-C第 http://blog.csdn.net/wzzvictory/article/details/8592492 增isa在在增第Person增 runtime的第NSKVONotifying_Person⾏行 NSKVONotifying_Person⼈人第 set增class增dealloc增_isKVOA⾏行
class
⼈人class⼈人下⾏行
道第
[java] view plaincopy
1. NSLog(@"self#>isa:%@",self#>isa);$$
2. NSLog(@"self$class:%@",[self$class]);$$ KVO增道第
[java] view plaincopy
1. self#>isa:Person$$
2. self$class:Person$$ KVO增道第
[java] view plaincopy
1. self#>isa:NSKVONotifying_Person$$
2. self$class:Person$$ isa在class新增⾏行
⼈人set ⼈人set增set⼼心第
[java] view plaincopy
1. #$(void)willChangeValueForKey:(NSString$*)key$$
2. #$(void)didChangeValueForKey:(NSString$*)key$$ 增didChangeValueForKey:美第
[java] view plaincopy
1. #$(void)observeValueForKeyPath:(NSString$*)keyPath$$
2. $$$$$$$$$$$$$$$$$$$$$$ofObject:(id)object$$
3. $$$$$$$$$$$$$$$$$$$$$$$$change:(NSDictionary$*)change$$
4. $$$$$$$$$$$$$$$$$$$$$$$context:(void$*)context$$
增KVO等 ⽽而家增-setValue:forKey第
[java] view plaincopy
1. #$(void)willChangeValueForKey:(NSString$*)key$$
2. #$(void)didChangeValueForKey:(NSString$*)key$$ 之得对⾃自⽽而增
起要只增KVO等 增KVO第 1)KVC
KVC增⽽而家增⽽而家will/ didChangeValueForKey:⽂文 ⽽而家增setValue:forKeywill/ didChangeValueForKey:⾏行
2)⽽而家
⼈人⽽而家will/didChangeValueForKey:⾏行
增⽽而家的得增KVO⾏行
3)起要will/didChangeValueForKey:⾏行
增KVO增will/didChangeValueForKey:⾏行
间_isKVOA
回可要 KVO ⾏行
1
么
两增
分来
活增AppleScript分来增过⾏行
2
KVC起增KVO增了去第
http://www.mikeash.com/pyblog/key-value-observing-done-right.html
KVO增selectorblock增
让⼈人-addObserver:forKeyPath:options:context:成⾏行
增MAKVONotificationCenter增
github:
https://github.com/mikeash/MAKVONotificationCenter
KVO多增信⾏行
了第
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/
KeyValueCoding/Articles/KeyValueCoding.html#//apple_ref/doc/uid/
10000107-SW1
http://blog.csdn.net/kesalin/article/details/8194240