Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

Key Paths

Like file paths (/usr/local/bin)

Used to traverse an object graph: "document.header.title"

valueForKeyPath:, setValue:forKeyPath:

IOS KVC

Warning!: you cannot pass a keyPath if the selector is just 'forKey:' only to 'forKeyPath:'

KVC

value types and nil

  • Property values are always of type id (reference type)
  • structs -> NSValue
  • numbers -> NSNumber
  • You should implement setNilValueForKey:

What is it?

Access properties by name:

Every class essentially becomes a dictionary

Accessor lookup

Not a 'trick'. This is fundamental Cocoa programming.

KVC Collection

Search for a public accessor ('getLastName' or 'lastName')

[person name]

Thank you for your attention!!

If not found, search for a private accessor method based on key ('_getLastName' or '_lastName').

[person valueForKey:@"name"]

[person setName:@"Yume"]

KVC Array

KVC NSDictionary

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

Collection Operators

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"]

How it works

NSKeyValueCoding (informal) protocol

Implemented by NSObject

NSKeyValueCoding.h

Reference :

http://bou.io/KVCCustomOperators.html#fn:3

http://www.slideshare.net/sergio.acosta/intro-to-cocoa-kvckvo-and-bindings-presentation

Learn more about creating dynamic, engaging presentations with Prezi