public
Created

Using KVO with NSSingleton

  • Download Gist
KVO + Singleton Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.