Core Data Transient Calculated Attributes

前端 未结 1 1992
面向向阳花
面向向阳花 2021-02-09 04:41

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 concatena

1条回答
  •  情书的邮戳
    2021-02-09 05:22

    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];
    }
    

    0 讨论(0)
提交回复
热议问题