依赖于自身的依赖属性的 KVO
我已经定义了两个具有相应 ivars 的属性并合成了它们:
@property (nonatomic,copy) NSString* title;
@property (nonatomic,copy) NSString* person;
现在在我的实现中,我为 title
提供了以下自定义 getter:
- (NSString*)title {
return (person) ? person : [title capitalizedString];
}
因此 title
属性取决于title
属性本身和 person
属性。我想让这个类 KVO 兼容,所以我添加了:
+ (NSSet*)keyPathsForValuesAffectingTitle {
return [NSSet setWithObjects:@"person", nil];
}
现在我的问题:我是否也必须将 @"title"
添加到集合中,以确保 的更改title
属性也被观察到了吗?如果是的话,这不会造成无限循环吗?
或者 KVO 是否自动依赖于属性本身?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,您不会将“title”属性放入集合中。我认为默认情况下所有属性都依赖于它们自身。
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.