Skip to content

Instantly share code, notes, and snippets.

@kevinlawler
Created July 7, 2012 06:18
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 kevinlawler/3065038 to your computer and use it in GitHub Desktop.
Save kevinlawler/3065038 to your computer and use it in GitHub Desktop.
Using KVO with NSSingleton
Bank *b = [Bank instance];
Person *p = [Person instance];
[b addObserver:p forKeyPath:@"accountBalance" options:NSKeyValueObservingOptionNew context:NULL];
b.accountBalance = [NSNumber numberWithInt:99];
#import "NSSingleton.h"
@interface Bank : NSSingleton
@property (nonatomic, strong) NSNumber *accountBalance;
@end
#import "Bank.h"
@implementation Bank
@synthesize accountBalance;
@end
#import "NSSingleton.h"
@interface Person : NSSingleton
@end
#import "Person.h"
@implementation Person
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"keypath: %@", keyPath);
NSLog(@"object: %@", [object description]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment