SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Multi-Level KVO
Jakub Hladík
Jakub Hladík
jakub@hippotaps.com
@ku33ing, @hippotaps
iOS app developer
hippotaps co-founder
iOS teacher at FIT CTU Prague
Key-Value Observing
What?
• Key-value observing is a mechanism that
allows objects to be notified of changes to
specified properties of other objects.
• alternatives you should already know:
• delegate/protocol
• NSNotification
• callback using @selector or block
9
Key-Value Coding First
• mechanism that allows accessing object
properties indirectly by a name/key (string)
• NSKeyValueCoding protocol
• NSObject provides default implementation
(getters/setters, …)
10
KVC How?
- (id)tableView:(NSTableView *)tableview
objectValueForTableColumn:(id)column row:(NSInteger)row
{
ChildObject *child = [childrenArray objectAtIndex:row];
if ([[column identifier] isEqualToString:@"name"]) {
return [child name];
}
if ([[column identifier] isEqualToString:@"age"]) {
return [child age];
}
if ([[column identifier] isEqualToString:@"favoriteColor"]) {
return [child favoriteColor];
}
}
11
KVC How?
- (id)tableView:(NSTableView *)tableview
objectValueForTableColumn:(id)column row:(NSInteger)row
{
ChildObject *child = [childrenArray objectAtIndex:row];
return [child valueForKey:[column identifier]];
}
12
KVC Terminology
• properties:
• attributes (scalar, string, NSNumber, …)
• to-one relationships (self.otherObject, self.superView, …)
• to-many relationships (collection of objects – NSArray,
NSSet, …)
• key (@”age”) – identifies object property
• path (@”superView.frame”, @”address.street”)
– dot.separated keys specifying a sequence of objects to
traverse
13
Getting/Setting
Attributes
– valueForKey:
– valueForKeyPath:
– dictionaryWithValuesForKeys:
– valueForUndefinedKey:
…
– setValue:forKeyPath:
– setValuesForKeysWithDictionary:
– setNilValueForKey:
– setValue:forKey:
– setValue:forUndefinedKey:
…
14
Getting/Setting
Attributes
@interface MyClass
@property NSString *stringProperty;
@property NSInteger integerProperty;
@property MyClass *linkedInstance;
@end
MyClass *myInstance = [[MyClass alloc] init];
NSString *string = [myInstance valueForKey:@"stringProperty"];
[myInstance setValue:@2 forKey:@"integerProperty"];
MyClass *anotherInstance = [[MyClass alloc] init];
myInstance.linkedInstance = anotherInstance;
[myInstance setValue:@2 forKeyPath:@"linkedInstance.integerProperty"];
15
Finally KVO
– observeValueForKeyPath:ofObject:change:context:
– addObserver:forKeyPath:options:context:
– removeObserver:forKeyPath:
– removeObserver:forKeyPath:context:
…
– willChangeValueForKey:
– didChangeValueForKey:
– willChange:valuesAtIndexes:forKey:
– didChange:valuesAtIndexes:forKey:
…
16
KVO
17
KVO
18
Simple Observing
[self setValue:[[self randomPhoneArray] mutableCopy]
forKey:@"dataArray"];
// self.dataArray = [[self randomPhoneArray] mutableCopy];
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"dataArray"]) {
self.dataArray = change[NSKeyValueChangeNewKey];
[self.tableView reloadData];
}
}
19
Demo
Simple Observing
HPT-Multi-Level-KVO
branch stage1
20
NSMutableArray
Observing
- (void)insertDataObject:(id)object
atIndex:(NSUInteger)index
{
[self willChange:NSKeyValueChangeInsertion
valuesAtIndexes:[NSIndexSet indexSetWithIndex:index]
forKey:@"dataArray"];
[self.dataArray insertObject:object
atIndex:index];
[self didChange:NSKeyValueChangeInsertion
valuesAtIndexes:[NSIndexSet indexSetWithIndex:index]
forKey:@"dataArray"];
}
21
NSMutableArray
Observing
- (void)removeDataObjectAtIndex:(NSUInteger)index
{
[self willChange:NSKeyValueChangeRemoval
valuesAtIndexes:[NSIndexSet indexSetWithIndex:index]
forKey:@"dataArray"];
[self.dataArray removeObjectAtIndex:index];
[self didChange:NSKeyValueChangeRemoval
valuesAtIndexes:[NSIndexSet indexSetWithIndex:index]
forKey:@"dataArray"];
}
22
NSMutableArray
Observing
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"dataArray"]) {
NSIndexSet *set = change[NSKeyValueChangeIndexesKey];
NSKeyValueChange valueChange = [change[NSKeyValueChangeKindKey] unsignedIntegerValue];
NSArray *new = change[NSKeyValueChangeNewKey];
switch (valueChange) {
case NSKeyValueChangeInsertion:
[self addObject:new.lastObject atIndex:set.lastIndex];
break;
case NSKeyValueChangeRemoval:
[self removeObjectAtIndex:set.lastIndex];
break;
case NSKeyValueChangeSetting:
self.dataArray = [new mutableCopy];
[self.tableView reloadData];
break;
default:
break;
}
}
}
23
Demo
NSMutableArray Change
Observing
HPT-Multi-Level-KVO
branch stage2
24
Array Observing
Simpler
- (void)insertDataObject:(id)object
atIndex:(NSUInteger)index
{
NSMutableArray *array = [self
mutableArrayValueForKey:@"dataArray"];
[array insertObject:object atIndex:index];
}
- (void)removeDataObjectAtIndex:(NSUInteger)index
{
NSMutableArray *array = [self
mutableArrayValueForKey:@"dataArray"];
[array removeObjectAtIndex:index];
}
25
Demo
NSMutableArray Change
Observing Done Better
HPT-Multi-Level-KVO
branch stage3
26
Nested Object
Observing
• NSMutableArray in NSMutableArray ):
• Not possible without ugly hacks…
• NSNotifications… (:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"objectInsertedAtIndexPath"
object:self
userInfo:@{ @"object" : object,
@"indexPath" : indexPath
}];
27
Multi-Level
Demo
Nested NSMutableArray Change
Observing using NSNotification
HPT-Multi-Level-KVO
branch stage4
28
Is That It?
29
No Way!
30
- (id)valueForUndefinedKey:(NSString *)key
{
NSUInteger i = [key integerValue];
return self.dataArray[i];
}
Model Change
- (id)valueForUndefinedKey:(NSString *)key
{
NSUInteger i = [key integerValue];
return self.dataArray[i];
}
- (void)insertDataObject:(id)object atIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == self.dataArray.count) {
[self insertDataObject:[NSMutableArray array] atIndex:indexPath.section];
}
[self willChange:NSKeyValueChangeInsertion
valuesAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.row]
forKey:[@(indexPath.section) description]];
NSMutableArray *array = self.dataArray[indexPath.section];
[array insertObject:object atIndex:indexPath.row];
[self didChange:NSKeyValueChangeInsertion
valuesAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.row]
forKey:[@(indexPath.section) description]];
}
31
Observer Change 1
- (void)addObserverForKey:(NSString *)key
{
[[HPTDataService sharedService] addObserver:self
forKeyPath:key
options:NSKeyValueObservingOptionNew
context:NULL];
}
- (void)removeObserverForLastKey
{
[[HPTDataService sharedService] removeObserver:self
forKeyPath:[@(self.dataArray.count-1) description]
context:NULL];
}
32
Observer Change 2
- (void)addObject:(id)object atIndex:(NSUInteger)index
{
[self.dataArray insertObject:object atIndex:index];
[self addObserverForKey:[@(self.dataArray.count-1) description]];
[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:index]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
33
Final Working
Multi-Level
KVO Demo
YEAH!
HPT-Multi-Level-KVO
branch stage5
34
The End
• Key-Value Observing Programming Guide
• Key-Value Coding Programming Guide
• https://github.com/kubbing/HPT-Multi-Level-KVO
• http://www.slideshare.net/JakubHladk/for-mobile-513
35

Contenu connexe

Tendances

JavaScript objects and functions
JavaScript objects and functionsJavaScript objects and functions
JavaScript objects and functionsVictor Verhaagen
 
Concept of constructors
Concept of constructorsConcept of constructors
Concept of constructorskunj desai
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 
GDG Madrid - Dart Event - By Iván Zaera
GDG Madrid - Dart Event - By Iván ZaeraGDG Madrid - Dart Event - By Iván Zaera
GDG Madrid - Dart Event - By Iván ZaeraJc Miñarro
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a BossBob Tiernay
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data TypesEelco Visser
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtimeDneprCiklumEvents
 
Introduction to JQ
Introduction to JQIntroduction to JQ
Introduction to JQKnoldus Inc.
 
Functions - complex first class citizen
Functions - complex first class citizenFunctions - complex first class citizen
Functions - complex first class citizenVytautas Butkus
 
_Function Builders in Swift #love_swift
_Function Builders in Swift #love_swift_Function Builders in Swift #love_swift
_Function Builders in Swift #love_swiftTomohiro Kumagai
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Writing Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel SchulhofWriting Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel SchulhofWithTheBest
 
Adopting Swift Generics
Adopting Swift GenericsAdopting Swift Generics
Adopting Swift GenericsMax Sokolov
 
Constructor&method
Constructor&methodConstructor&method
Constructor&methodJani Harsh
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)PROIDEA
 
The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196Mahmoud Samir Fayed
 

Tendances (20)

JavaScript objects and functions
JavaScript objects and functionsJavaScript objects and functions
JavaScript objects and functions
 
Concept of constructors
Concept of constructorsConcept of constructors
Concept of constructors
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Slickdemo
SlickdemoSlickdemo
Slickdemo
 
GDG Madrid - Dart Event - By Iván Zaera
GDG Madrid - Dart Event - By Iván ZaeraGDG Madrid - Dart Event - By Iván Zaera
GDG Madrid - Dart Event - By Iván Zaera
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data Types
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtime
 
Introduction to JQ
Introduction to JQIntroduction to JQ
Introduction to JQ
 
Functions - complex first class citizen
Functions - complex first class citizenFunctions - complex first class citizen
Functions - complex first class citizen
 
_Function Builders in Swift #love_swift
_Function Builders in Swift #love_swift_Function Builders in Swift #love_swift
_Function Builders in Swift #love_swift
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Writing Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel SchulhofWriting Node.js Bindings - General Principles - Gabriel Schulhof
Writing Node.js Bindings - General Principles - Gabriel Schulhof
 
Adopting Swift Generics
Adopting Swift GenericsAdopting Swift Generics
Adopting Swift Generics
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196The Ring programming language version 1.7 book - Part 41 of 196
The Ring programming language version 1.7 book - Part 41 of 196
 

Similaire à For mobile 5/13'

iOS Einführung am Beispiel von play NEXT TEE
iOS Einführung am Beispiel von play NEXT TEEiOS Einführung am Beispiel von play NEXT TEE
iOS Einführung am Beispiel von play NEXT TEEHendrik Ebel
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4Calvin Cheng
 
iOS for ERREST - alternative version
iOS for ERREST - alternative versioniOS for ERREST - alternative version
iOS for ERREST - alternative versionWO Community
 
Beginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCABeginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCAWhymca
 
EclipseCon2011 Cross-Platform Mobile Development with Eclipse
EclipseCon2011 Cross-Platform Mobile Development with EclipseEclipseCon2011 Cross-Platform Mobile Development with Eclipse
EclipseCon2011 Cross-Platform Mobile Development with EclipseHeiko Behrens
 
Practical Model View Programming (Roadshow Version)
Practical Model View Programming (Roadshow Version)Practical Model View Programming (Roadshow Version)
Practical Model View Programming (Roadshow Version)Marius Bugge Monsen
 
Developing iOS REST Applications
Developing iOS REST ApplicationsDeveloping iOS REST Applications
Developing iOS REST Applicationslmrei
 
Swipe 2011 - iOS Gems
Swipe 2011 - iOS GemsSwipe 2011 - iOS Gems
Swipe 2011 - iOS GemsKevin O'Neill
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++nsm.nikhil
 
Migration Objective-C to Swift
Migration Objective-C to SwiftMigration Objective-C to Swift
Migration Objective-C to SwiftNattapon Nimakul
 
iOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core DataiOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core DataChris Mar
 
Nu program language on Shibuya.lisp#5 LT
Nu program language on  Shibuya.lisp#5 LTNu program language on  Shibuya.lisp#5 LT
Nu program language on Shibuya.lisp#5 LTYuumi Yoshida
 
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5Webエンジニアから見たiOS5
Webエンジニアから見たiOS5Satoshi Asano
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++vidyamittal
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Getachew Ganfur
 
Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법Jung Kim
 
Swift after one week of coding
Swift after one week of codingSwift after one week of coding
Swift after one week of codingSwiftWro
 

Similaire à For mobile 5/13' (20)

iOS Einführung am Beispiel von play NEXT TEE
iOS Einführung am Beispiel von play NEXT TEEiOS Einführung am Beispiel von play NEXT TEE
iOS Einführung am Beispiel von play NEXT TEE
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4
 
iOS for ERREST - alternative version
iOS for ERREST - alternative versioniOS for ERREST - alternative version
iOS for ERREST - alternative version
 
Beginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCABeginning icloud development - Cesare Rocchi - WhyMCA
Beginning icloud development - Cesare Rocchi - WhyMCA
 
EclipseCon2011 Cross-Platform Mobile Development with Eclipse
EclipseCon2011 Cross-Platform Mobile Development with EclipseEclipseCon2011 Cross-Platform Mobile Development with Eclipse
EclipseCon2011 Cross-Platform Mobile Development with Eclipse
 
Practical Model View Programming (Roadshow Version)
Practical Model View Programming (Roadshow Version)Practical Model View Programming (Roadshow Version)
Practical Model View Programming (Roadshow Version)
 
I os 04
I os 04I os 04
I os 04
 
Developing iOS REST Applications
Developing iOS REST ApplicationsDeveloping iOS REST Applications
Developing iOS REST Applications
 
Swipe 2011 - iOS Gems
Swipe 2011 - iOS GemsSwipe 2011 - iOS Gems
Swipe 2011 - iOS Gems
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
 
201104 iphone navigation-based apps
201104 iphone navigation-based apps201104 iphone navigation-based apps
201104 iphone navigation-based apps
 
Migration Objective-C to Swift
Migration Objective-C to SwiftMigration Objective-C to Swift
Migration Objective-C to Swift
 
iOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core DataiOSDevCamp 2011 Core Data
iOSDevCamp 2011 Core Data
 
Nu program language on Shibuya.lisp#5 LT
Nu program language on  Shibuya.lisp#5 LTNu program language on  Shibuya.lisp#5 LT
Nu program language on Shibuya.lisp#5 LT
 
Webエンジニアから見たiOS5
Webエンジニアから見たiOS5Webエンジニアから見たiOS5
Webエンジニアから見たiOS5
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
 
Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01Cppt 101102014428-phpapp01
Cppt 101102014428-phpapp01
 
Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법Swift와 Objective-C를 함께 쓰는 방법
Swift와 Objective-C를 함께 쓰는 방법
 
Swift after one week of coding
Swift after one week of codingSwift after one week of coding
Swift after one week of coding
 
UIWebView Tips
UIWebView TipsUIWebView Tips
UIWebView Tips
 

For mobile 5/13'