Search code examples
iphonecocoadependency-propertieskey-value-observing

KVO for dependent property depending on itself


I've defined two properties with corresponding ivars and synthesized them:

@property (nonatomic,copy) NSString* title;
@property (nonatomic,copy) NSString* person;

Now in my implementation, I've the following custom getter for title:

- (NSString*)title {
    return (person) ? person : [title capitalizedString];
}

So the title property depends on both the title property itself and the person property. I'd like to make this class KVO compatible, so I added:

+ (NSSet*)keyPathsForValuesAffectingTitle {
    return [NSSet setWithObjects:@"person", nil];
}

Now my question: Do I have to add @"title" to the set as well, to make sure changes of the title property are observed, too? If yes, doesn't this create an infinite loop?

Or does KVO automatically depend on the property itself?


Solution

  • According to the docs, you don't put the 'title' property in the set. I presume it's assumed that all properties are, by default, dependent upon themselves.