2012-06-13 46 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; 
    } 

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