user4951
user4951

Reputation: 33130

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

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?

Upvotes: 0

Views: 116

Answers (2)

Josh
Josh

Reputation: 676

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

Upvotes: 1

Alex
Alex

Reputation: 393

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

Upvotes: 1

Related Questions