从keypath恢复NSEntityDescription

时间:2012-06-13 08:20:43

标签: core-data

假设我有核心数据实体A和B,其中B通过属性a指向A.

鉴于B的NSEntityDescription和A的给定属性的关键路径(例如@“a.name”),有没有办法恢复A的NSEntityDescription?

谢谢,

1 个答案:

答案 0 :(得分: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;
    } 

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