objective c - When is KVC & KVO worth the trouble? Always? -


it seems new superset on objective-c, combinations of dot notation mixed directives, e.g.: studentsinclassa.@union.studentsinclassb.pets(...) , compliance syntax, e.g.: -replaceobjectin<key>atindex:withobject:. seems large portion of kvc akin simple accessor methods, can synthesized anyway. however, kvobserving seems make mvc apps easier. opinions?

key-value coding allows arbitrarily nested attributes identity known @ runtime. example, kvc not replacement person.name — it's needlessly generic such specific task. let's didn't know when writing our program whether wanted name, age or favorite shoe brand. run sort of thing quite nstableview data sources. write big, repetitive conditional send message want, kvc makes easy:

return [personcontroller.selectedperson valueforkeypath:desiredattribute]; 

then can set desiredattribute @"name", @"age" or @"favoriteshoebrand.name" , we'll correct value without branching though know attribute want @ runtime.

it's useful because classes can handle kvc in special ways. example, nsarray nice. [arrayofpeople valueforkeypath:@"name"] not equivalent arrayofpeople.name — instead, passes on keypath each object , creates new array results of doing that. serves same purpose map function in many other languages. there special kvc operators, such @distinctunionofarrays (which takes collection of arrays , merges them together, ignoring duplicates), make operating on nested collections way more concise otherwise be.

as kvo, way of getting notified when changes — so, example, can update ui new age on person's birthday without ui needing know birthday logic. if sounds useful you, you'll it.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -