开发者

IOS Core Data with KVO - valueForKey: for custom property on an NSManagedObject subclass?

开发者 https://www.devze.com 2023-03-09 07:04 出处:网络
I have two NSManagedObject subclasses derived from entities, ActivityMember and Member. Member has a property called name, and ActivityMember, through a one to one relationship with Member, has a prop

I have two NSManagedObject subclasses derived from entities, ActivityMember and Member. Member has a property called name, and ActivityMember, through a one to one relationship with Member, has a property called member. I am attempting to create a custom property on ActivityMember called name that simply exposes its member's name, like so:

ActivityMember.m:

-(NSString *)memberName
{
    if (!self.member) {
        return nil;
    }
    return self.member.name;
}

ActivityMember.h:

@property (readonly) NSString *memberName;

When my TableViewController subclass sends valueForKey: to an instance of ActivityMember, it fails with the following message:

[<NSManagedObject 0x4d6c830> valueForUndefinedKey:]: the entity ActivityMember is not key value coding-compliant for the key "memberName".

If I add a transient memberName attribute to ActivityMember, it no longer fails, but the memberName property is always nil, even though self.member.name evaluates to a valid string.

Thinking transient attributes might need some automatic updating, I also added the following to ActivityMember, with no success.

+ (NSSet *)keyPathsForValuesAffectingMemberName
{
    return [NSSet setWithObjects:@"member.name", nil];
}

Is it possible NSManagedObject does its own KVO magic? I set a breakpoint inside the body of keyPathsForValuesAffectingMemberName, but it never broke.

This seems like a common use case, but I开发者_StackOverflow haven't found a good post on the subject.


This one turned out to be pilot error. In my data model, I had not specified the class name for ActivityMember. Subtle, since other properties worked fine (the magic of reflection).

0

精彩评论

暂无评论...
验证码 换一张
取 消