2012-06-13 42 views
1

假設我有核心數據實體A和B,其中B通過屬性a指向A.從關鍵路徑中恢復NSEntityDescription

鑑於B的NSEntityDescription和給定屬性A(例如@「a.name」)的關鍵路徑,是否有任何方法可以恢復A的NSEntityDescription?

感謝,

回答

1

我通過分析關鍵路徑自己設法到這一點:

// Split the path to the section name up 
    NSArray *keyNameParts = [sectionNameKeyPath componentsSeparatedByString:@"."]; 

    // Follow this back to the Entity description for the Section Entity 
    for (int idx = 0; idx < keyNameParts.count - 1; idx++) { 
     NSDictionary *relationships = entityDescription.relationshipsByName; 
     NSString *partName = [keyNameParts objectAtIndex:idx]; 
     NSRelationshipDescription *relationshipDescription = [relationships objectForKey:partName]; 
     if (!relationshipDescription) 
     { 
      [NSException raise:@"Relationship not found for keypath" 
         format:@"Entity '%@' does not point to a relationshop for '%@' in keypath '@'.", entityName, partName, sectionNameKeyPath]; 
     } 
     entityDescription = relationshipDescription.entity; 
    } 

如果有我很想知道它一個更直接的方法。