2011-05-29 102 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屬性。結果將是你需要的。