Skip to content

Instantly share code, notes, and snippets.

@hashier
Last active December 19, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashier/75281a806d85468a8cb9 to your computer and use it in GitHub Desktop.
Save hashier/75281a806d85468a8cb9 to your computer and use it in GitHub Desktop.
KVC / KVO
//
// main.m
// KVO
//
// Created by Christopher Loessl on 7/14/13.
// Copyright (c) 2013 Christopher Loessl. All rights reserved.
//
/*
* The code is based on a tutorial from:
* http://www.andyibanez.com/74-key-value-coding-key-value-observing-and-cocoa-bindings-a-basic-tutorial
* but since it uses old Objective C style I updated it.
* If you don't understand the code directly it might be a good idea to
* have a look there since the explaination of how KVO and KVC works is good.
*/
/*
$ clang -framework Foundation main.m -o main
$ ./main
2013-07-14 23:13:15.189 main[26164:707] Sakura Kinomoto has 20 Clow Cards
2013-07-14 23:13:15.192 main[26164:707] Li Shaoran has 21 Clow Cards
2013-07-14 23:13:15.193 main[26164:707] Sakura Kinomoto has earned a card! Cards now: 22
*/
#import <Foundation/Foundation.h>
@interface Character : NSObject
@property (nonatomic, strong) NSString *characterName;
@property (nonatomic, strong) NSNumber *ownedClowCards;
- (void)hasLostClowCard;
- (void)hasGainedClowCard;
@end
@implementation Character
- (void)hasLostClowCard
{
NSLog(@"%@ has lost a card! Cards now: %@", self.characterName, self.ownedClowCards);
}
- (void)hasGainedClowCard
{
NSLog(@"%@ has earned a card! Cards now: %@", self.characterName, self.ownedClowCards);
}
//---------------------------------------------------------------------
// method that get's executed if one of the observing values changes
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"ownedClowCards"]) {
if (change[NSKeyValueChangeNewKey] < change [NSKeyValueChangeOldKey]) {
[self hasLostClowCard];
} else {
[self hasGainedClowCard];
}
}
}
@end
int main()
{
@autoreleasepool {
Character *sakura;
Character *shaoran;
//Create and give the properties some values with KVC...
sakura = [[Character alloc] init];
sakura.characterName = @"Sakura Kinomoto";
sakura.ownedClowCards = @20;
shaoran = [[Character alloc] init];
shaoran.characterName = @"Li Shaoran";
shaoran.ownedClowCards = @21;
NSLog(@"%@ has %@ Clow Cards", sakura.characterName, sakura.ownedClowCards);
NSLog(@"%@ has %@ Clow Cards", shaoran.characterName, shaoran.ownedClowCards);
//---------------------------------------------------------------------
// Here begins the KVO section.
// register KVO
// Sakura is observing her own Clow Cards.
[sakura addObserver:sakura
forKeyPath:@"ownedClowCards"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
// observing value changes so
// observeValueForKeyPath:ofObject:change:context: gets executed
sakura.ownedClowCards = @22;
// unregister KVO
[sakura removeObserver:sakura forKeyPath:@"ownedClowCards"];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment