Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
Like file paths (/usr/local/bin)
Used to traverse an object graph: "document.header.title"
valueForKeyPath:, setValue:forKeyPath:
Warning!: you cannot pass a keyPath if the selector is just 'forKey:' only to 'forKeyPath:'
Access properties by name:
Every class essentially becomes a dictionary
Not a 'trick'. This is fundamental Cocoa programming.
Search for a public accessor ('getLastName' or 'lastName')
[person name]
If not found, search for a private accessor method based on key ('_getLastName' or '_lastName').
[person valueForKey:@"name"]
[person setName:@"Yume"]
If not found' search for an instance variable (_lastName or lastName)
[person setValue:@"Yume"
ForKey:@"name"]
No native support. You cannot pass indexes in key paths:
valueForKeyPath:@"order.items[1].price"
If not found, invoke valueForUndefinedKey:
NSDictionary has a custom implementation of KVC that attempts to match keys against keys in the dictionary.
Ask an array for a key, the array will ask all its elements and return an array of the resulting values.
NSDictionary *dict = @{@"name":@"Dream"};
[dictionary setValue:@"Yume" forKey:@"name"]
KVC
Custom Collections Operators
Format :
- (id) _<operator>ForKeyPath:(NSString*)keyPath
Example :
- (id) _timesForKeyPath:(NSString*)keyPath
@"aaa.bbb"
@times
[array valueForKeyPath:@"@times.aaa.bbb"]
@avg, @count, @max, @min, @sum
@distinctUnionOfArrays, @distinctUnionOfObjects, @distinctUnionOfSets, @unionOfArrays,
@unionOfObjects,
@unionOfSets
[NSObject self]
[@[@1,@2,@3] valueForKeyPath:@"@max.self"]
Collection Method
[@[@1,@2,@3] valueForKey:@"@count"]
Content Method
[@[@1,@2,@3] valueForKey:@"intValue"]
NSKeyValueCoding (informal) protocol
Implemented by NSObject
NSKeyValueCoding.h