码迷,mamicode.com
首页 > 其他好文 > 详细

KVO与KVC整理资料

时间:2015-11-06 09:39:29      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

KVC/KVO??????????????????

????第wangzz ????????第http://blog.csdn.net/wzzvictory/article/details/9674431 ?????????????? ????????????????????到?生增?????????????????????????手??wangzzstrive?? ??????增????等

????第 1????????????你KVC/KVO??????增????????????看????????????真以???? ???行 2??????KVO????????增??对????KVC????????????增??????????????KVC?? ???行

????????

KVC/KVO??中种??开就??????????增??Cocoa??????????????微NSObject?? ??????NSKeyValueCoding/NSKeyValueObserving????就样??????就???????? ?????????????????行??样????????????增KVC/KVO?????????????????????? ????主???????????????行 ????增KVC/KVO????????????Objective-C?????大????runtime增???????????? ????????????只?行 ?心??增KVC/KVO???????????而??家??????????增????????????????学?行

1??KVC????
??????Key-value coding增??全??之得?????行????????增??时??????????map

??????经活?行???????????????????力觉???????而??家???????而????????????两
???????????行

2??KVO????
??????Key-value observing增??全??之得中种?行??????????????????????????

还的???????????????????????????行??MVC??????????Cocoa??增KVO??????

?自??????model??controller?????????????行

????KVC????????

1??Key??Key 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?????????????????????? ??给???而??时?????????????????人?????????力觉?行

2??????????KVC

?????????而??家??????????增??????来????KVC?而??????????????????增???? ?????????????行?????????而??????????????增??来??????????增????KVC???? ???????行明??????????????????????第KVC?????而??????得?行?? 3??????????????To-Many???????????????????? ??????????????????????????????????????明To-One??,????Person?????? name????增???????????????????行??????????????????增????Person?????? ??friendsName????增???????子??明??Objective-C????????NSArray增NSSet ????增??说???????????????????????????行 ??给????????????????????????增??????????????第

??????给??

有????KVC?????????子??????增?????????子??????给???子???????????行

??????给??

????????????????????????开发增??????????后?????一就??????????????????
?而???子??????????????不???行

??情?子??????????????第

[java] view plaincopy

  1. #countOf<Key>$$

  2. //????????????????NSArray??????????count:$$

  3. #objectIn<Key>AtIndex:$$

  4. #<key>AtIndexes:$$

  5. //??????????????????????????$NSArray$????

    ??$objectAtIndex:$??$objectsAtIndexes:$$

  6. #get<Key>:range:$$

  7. //??????????????????????????????????????????????$NSArray$?? ??$getObjects:range:$$

  8. $$

  9. #insertObject:in<Key>AtIndex:$$

  10. #insert<Key>:atIndexes:$$

  11. //????????????????????????$NSMutableArray$????

    ??$insertObject:atIndex:$??$insertObjects:atIndexes:$$

  12. #removeObjectFrom<Key>AtIndex:$$

  13. #remove<Key>AtIndexes:$$

  14. //????????????????????????$NSMutableArray$????

    ??$removeObjectAtIndex:$??$removeObjectsAtIndexes:$$

  15. #replaceObjectIn<Key>AtIndex:withObject:$$

  16. #replace<Key>AtIndexes:with<Key>:$$

  17. //????????????????????????????????????????????????????$$

??情?子??????????????第

[java] view plaincopy

  1. #countOf<Key>$$

  2. //????????????????NSArray??????????count:$$

  3. #objectIn<Key>AtIndex:$$

  4. #<key>AtIndexes:$$

  5. //??????????????????????????$NSArray$????

    ??$objectAtIndex:$??$objectsAtIndexes:$$

  6. #get<Key>:range:$$

  1. //??????????????????????????????????????????????$NSArray$?? ??$getObjects:range:$$

  2. $$

  3. #insertObject:in<Key>AtIndex:$$

  4. #insert<Key>:atIndexes:$$

  5. //????????????????????????$NSMutableArray$????

    ??$insertObject:atIndex:$??$insertObjects:atIndexes:$$

  6. #removeObjectFrom<Key>AtIndex:$$

  7. #remove<Key>AtIndexes:$$

  8. //????????????????????????$NSMutableArray$????

    ??$removeObjectAtIndex:$??$removeObjectsAtIndexes:$$

  9. #replaceObjectIn<Key>AtIndex:withObject:$$

  10. #replace<Key>AtIndexes:with<Key>:$$

  11. //??????????????????????????????????????????????????????????????$$

??????????????????????????增??????????????????????????增???????????? ???用都增?????????????????行得??在??????增????友?????????????????????? 个??????就样??????就????增????????????在我???????????行 4????????????Key-Value Validation?? KVC??????们??Key??????Value??????????????第

[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

  1. #(BOOL)validateName:(id$*)ioValue$error: (NSError$*$__autoreleasing$*)outError${$$

  2. $$$$//$Implementation$specific$code.$$

  3. $$$$return$...;$$

  4. }$$

??????????????????然?????????行

????在??????增KVC??????????????之得们????????增??????????????????们

???行????????真以增????CoreData???????????行

5??KVC??????????????????????????

????????????????????得????如????????着增????????????????????????次?行

????????KVC?????????????????????????????行KVC??????????????得????如

????????着????????????NSNumber??NSValue????增???????自????不???行

????两??增Person????????NSInteger??????age????

??还的得

????????KVC真以??????????就想??age??????得第 [java] view plaincopy

1. [person$setValue:[NSNumber$numberWithInteger:5]$forKey:@"age"];$$

????????age????????NSNumber????增KVC??????????NSNumber??????也 ??NSInteger????增?????????????????而??家????想??age??得?行

??????得 ????增????????就????age????得第

[java] view plaincopy
1. [person$valueForKey:@"age"];$$

????增????NSNumber????就????age??得?行

????????????增????????????????NSNumber增????????????????NSValue意

间????NSNumber????

????????NSNumber????着??????第 [java] view plaincopy

  1. +$(NSNumber$*)numberWithChar:(char)value;$$

  2. +$(NSNumber$*)numberWithUnsignedChar:(unsigned$char)value;$$

  3. +$(NSNumber$*)numberWithShort:(short)value;$$

  4. +$(NSNumber$*)numberWithUnsignedShort:(unsigned$short)value;$$

  5. +$(NSNumber$*)numberWithInt:(int)value;$$

  6. +$(NSNumber$*)numberWithUnsignedInt:(unsigned$int)value;$$

  7. +$(NSNumber$*)numberWithLong:(long)value;$$

  8. +$(NSNumber$*)numberWithUnsignedLong:(unsigned$long)value;$$

  9. +$(NSNumber$*)numberWithLongLong:(long$long)value;$$

  10. +$(NSNumber$*)numberWithUnsignedLongLong:(unsigned$long$long)value;$$

  11. +$(NSNumber$*)numberWithFloat:(float)value;$$

  12. +$(NSNumber$*)numberWithDouble:(double)value;$$

  13. +$(NSNumber$*)numberWithBool:(BOOL)value;$$

  14. +$(NSNumber$*)numberWithInteger:

         (NSInteger)value$NS_AVAILABLE(10_5,$2_0);$$
    
  15. +$(NSNumber$*)numberWithUnsignedInteger:

         (NSUInteger)value$NS_AVAILABLE(10_5,$2_0);$$
    

????????????????????得????着?行 ??????NSValue????

NSValue??????????????如????????着增?????????????????子????如??????第 [java] view plaincopy

  1. +$(NSValue$*)valueWithCGPoint:(CGPoint)point;$$

  2. +$(NSValue$*)valueWithCGSize:(CGSize)size;$$

  3. +$(NSValue$*)valueWithCGRect:(CGRect)rect;$$

  4. +$(NSValue$*)valueWithCGAffineTransform:

         (CGAffineTransform)transform;$$
    
  5. +$(NSValue$*)valueWithUIEdgeInsets:(UIEdgeInsets)insets;$$

  6. +$(NSValue$*)valueWithUIOffset:

         (UIOffset)insets$NS_AVAILABLE_IOS(5_0);$$
    

??????????6??????等????????????????????如??????会意??????增??????如 ????????????????NSValue??????增????????????了????????????去????第 http://blog.csdn.net/wzzvictory/article/details/8614433 6??????????????Collection 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"];$$

??????????好????机????????能??????????增??????????????????好????机 ???行
间Array??Set给???力 ??????????能点??增???????子???????无?子????????增????现????????????上?? ??第

[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. $$$

  1. //$Add$a$second$array$of$objects;$this$array$contains$alternate$val ues.$$

  2. [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"];$$ ????????????

1??KVC?????????????? 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: ?????行??????????很??????????????????增??????????着???????人?????行 2??KVC/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:forKey??????????will/ 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??????????增??????????selector????block????????增???? 让???人-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 

KVO与KVC整理资料

标签:

原文地址:http://www.cnblogs.com/fengwuqingyang/p/4941578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!