Skip to content

Instantly share code, notes, and snippets.

@jdelStrother
Created August 21, 2014 10:09
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 jdelStrother/cb138903fce203704261 to your computer and use it in GitHub Desktop.
Save jdelStrother/cb138903fce203704261 to your computer and use it in GitHub Desktop.
@interface Person : NSObject
@property NSString* firstName, *lastName;
-(NSString*)fullName;
@end
@implementation Person
+ (NSSet *)keyPathsForValuesAffectingFullName {
return [NSSet setWithObjects:@"lastName", @"firstName", nil];
}
-(NSString*)fullName {
return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
@end
Person* person = [[Person alloc] init];
person.firstName = @"jon";
person.lastName = @"harris";
[person addObserver:self forKeyPath:@"fullName.length" options:0 context:kvoContext];
person.firstName = @"john"; // exception :
// *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot update for observer <ViewController 0x7fac03659d50> for the key path "fullName.length" from <Person 0x7fac035317c0>, most likely because the value for the key "fullName" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the Person class.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment