开发者

Getting All Objects in a Relationship Based on Name of Relationship in CoreData

开发者 https://www.devze.com 2023-03-09 05:30 出处:网络
Say I have a coreData object named Business. Business have a bunch of to-many relationship. Business has Phones, Images, Districts, URLs, etc.

Say I have a coreData object named Business. Business have a bunch of to-many relationship.

Business has Phones, Images, Districts, URLs, etc.

I know can I do NSSet somePhones = self.Phones?

What about if I want to create a function, given the name of the relationship, namely @开发者_如何转开发"Phones" will give me that same set.

So NSSet * getManyRelationship:(NSString relationshipName)

Can I pull that out?


Define a method in your NSManagedObject's category that looks like this:

- (NSSet *)getManyRelationship:(NSString *)relationshipName{
    // insert sanity checking for relationshipName form
    // needs to be camelcase:
    //     'photos' rather than 'Photos'
    //     'userPhotos' rather than 'UserPhotos' or 'userphotos'
    SEL method = NSSelectorFromString(relationshipName);
    if([workout respondsToSelector:method])
        return objc_msgSend(self, method);
    return nil;
}


You can access to all attribute names by [Business attributeKeys] and check isTooMany property for any attribute. Result will be what u need.

0

精彩评论

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