核心数据瞬态计算属性

发布于 2024-11-08 04:22:45 字数 354 浏览 0 评论 0原文

我有一个包含lastName 和firstName 属性的实体。由于超出此问题范围的原因,我想要一个 fullName 属性,该属性作为firstName + space + lastName 的串联来计算。

因为这纯粹是一个计算值,不需要重做/撤消或瞬态属性的任何其他更复杂的方面(合并等),所以我的直觉告诉我只需重写 getter 方法即可返回所述计算值。阅读表明,如果我这样做,我唯一关心的是它是否符合 KVO,我可以通过使用 keyPathsForValuesAffectingVolume 来解决这个问题,以确保对firstName或lastName的更改为观察fullName的任何人触发通知。

我错过了什么吗?我正在检查,因为我是这个环境的初学者。

I have an entity that contains lastName and firstName attributes. For reasons beyond the scope of this question, I want a fullName attribute that gets calculated as a concatenation of firstName + space + lastName.

Because this is purely a calculated value, with no need for redo/undo or any other of the more sophisticated aspects of transient attributes (merging, etc.), my gut tells me to just override the getter method to return said calculated value. Reading suggests that, if I do this, my only concern would be whether it's KVO compliant, which I can address by using keyPathsForValuesAffectingVolume to ensure changes to firstName or lastName trigger notifications for anyone observing on fullName.

Am I missing anything? I'm checking because I'm a beginner to this environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-11-15 04:22:45

我对此也很陌生,所以我不完全确定我的答案,但据我了解,你是对的。

- (NSString *)fullName
{
    [self willAccessValueForKey:@"fullName"];
    NSString *tmp = [self primitiveFullName];
    [self didAccessValueForKey:@"fullName"];

    if (!tmp) {
        tmp = [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]];
        [self setPrimitiveFullName:tmp];
    }
    return tmp;
}

- (void)setFirstName:(NSString *)aFirstName
{
    [self willChangeValueForKey:@"firstName"];
    [self setPrimitiveFirstName:aFirstName];
    [self didChangeValueForKey:@"firstName"];

    [self setPrimitiveFullName:nil];
}

- (void)setLastName:(NSString *)aLastName
{
    [self willChangeValueForKey:@"lastName"];
    [self setPrimitiveLastName:aLastName];
    [self didChangeValueForKey:@"lastName"];

    [self setPrimitiveFullName:nil];
}

+ (NSSet *)keyPathsForValuesAffectingFullName
{
    return [NSSet setWithObjects:@"firstName", @"lastName", nil];
}

I'm also new to this, so I'm not completely sure about my answer, but as I understand it you are correct.

- (NSString *)fullName
{
    [self willAccessValueForKey:@"fullName"];
    NSString *tmp = [self primitiveFullName];
    [self didAccessValueForKey:@"fullName"];

    if (!tmp) {
        tmp = [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]];
        [self setPrimitiveFullName:tmp];
    }
    return tmp;
}

- (void)setFirstName:(NSString *)aFirstName
{
    [self willChangeValueForKey:@"firstName"];
    [self setPrimitiveFirstName:aFirstName];
    [self didChangeValueForKey:@"firstName"];

    [self setPrimitiveFullName:nil];
}

- (void)setLastName:(NSString *)aLastName
{
    [self willChangeValueForKey:@"lastName"];
    [self setPrimitiveLastName:aLastName];
    [self didChangeValueForKey:@"lastName"];

    [self setPrimitiveFullName:nil];
}

+ (NSSet *)keyPathsForValuesAffectingFullName
{
    return [NSSet setWithObjects:@"firstName", @"lastName", nil];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文