| Start | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Most Common Real Life Key Value Coding Example Key Value Coding is integrated into NSObject using NSKeyValueCoding protocol. What this means? It means that any id object is capable of calling valueForKey method and its various variants like valueForKeyPath etc. ' It also means that any id object can invoke setValue method and its various variants too. Example: Exceptions: Above example assumes that MyClass has an NSNumber Property called myNumber. If myNumber does not appear in MyClass interface definition, an NSUndefinedKeyException can be raised at possibly both lines 2 and 5 - popularly known as: Why this is SO powerful: You can write code that can access properties of a class dynamically, without needing interface for that class. This means that a table view can display values from any properties of an NSObject derived object, provided its property names are supplied dynamically at runtime. In the example above, the code can as well work without MyClass being available and id type obj being available to calling code. |
||||||||||||||||||||||
| Key Value Coding / Key Value Observing | ||||||||||||||||||||||
Querying KVC Data You can query values stored using KVC quickly and easily, without needing to retrieve or cast these as local variables. |
||||||||||||||||||||||
| Collection Operators Collection Operators can be used in a KVC key path to perform an operation on a “collection-type” property (i.e. While this is completely redundant here (we could have just accessed the
In just a 4 lines of code and pure Foundation, with the power of Key-Value Coding collection operators we were able to extract a rectangle that encapsulates all of the points in our array. It is important to note that these comparisons are made by invoking the
Here, we used Finally, there's the
And finally, the last 3 operators in the The one missing from this example is And that's it! Collection operators can be really powerful if used correctly, and can help to avoid having to loop through stuff unnecessarily. One last note: you can also use the standard collection operators on arrays of |
Read Article
https://develop.horje.com/learn/1434/reference