2011-05-29 66 views
0

假设我有一个名为Business的coreData对象。 Business有一堆对多的关系。基于CoreData中的关系名称获取关系中的所有对象

BusinessPhonesImagesDistrictsURLs

我知道我可以做NSSet somePhones = self.Phones

如果我想创建一个函数,给定关系的名称,即@“Phones”会给我同样的设置。

所以NSSet * getManyRelationship:(NSString relationshipName)

我可以拉说出来?

回答

1

定义您NSManagedObject的分类方法,看起来像这样:

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

您可以通过[业务属性键] 访问所有属性名称,并检查任何属性的isTooMany属性。结果将是你需要的。