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?
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.
精彩评论