Skip to content

Instantly share code, notes, and snippets.

@stith
Created January 20, 2013 23:01
Show Gist options
  • Save stith/4582429 to your computer and use it in GitHub Desktop.
Save stith/4582429 to your computer and use it in GitHub Desktop.
@implementation Observer
-(id)init {
// ...
[_preferences addObserver:self forKeyPath:@"apiKey" options:NSKeyValueObservingOptionNew context:nil];
[_preferences addObserver:self forKeyPath:@"apiSecret" options:NSKeyValueObservingOptionNew context:nil];
}
// ...
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"apiKey"]) {
_apiKey = [change objectForKey:NSKeyValueChangeNewKey];
} else if ([keyPath isEqualToString:@"apiSecret"]) {
_apiSecret = [change objectForKey:NSKeyValueChangeNewKey];
}
MSLog(@"Secret: %@", _apiSecret);
MSLog(@"Change: %@:%@", keyPath, changed);
}
@end
@class EMGenericKeychainItem;
@interface Preferences : NSObject {
@private
EMGenericKeychainItem *_keychainItem;
}
+ (id)preferences;
@property (nonatomic, retain) NSString *apiKey;
@property (nonatomic, retain) NSString *apiSecret;
@end
@implementation Preferences
@synthesize apiKey = _apiKey;
@synthesize apiSecret = _apiSecret;
// ...
-(NSString*)apiSecret {
return _keychainItem.password;
}
-(void)apiSecret:(NSString *)newApiSecret {
MSLog(@"Changed: %@", newApiSecret);
if (newApiSecret && ![newApiSecret isKindOfClass:[NSNull class]]) {
_keychainItem.password = newApiSecret;
} else {
_keychainItem.password = @"";
}
_apiSecret = newApiSecret;
}
// ...
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment