NSManagedObject层次结构导入和导出

我正在努力使我的NSMangedObjectClass配置文件可以导出。 我这样试试 如果我在NSArrays中编写关系,导出工作正常,因为NSSet没有实现
writeToFile
- (void) exportProfile:(Profile *)profile toPath:(NSString *)path{
//Profile
NSMutableDictionary *profileDict = [[self.selectedProfile dictionaryWithValuesForKeys:[[[self.selectedProfile entity] attributesByName] allKeys]] mutableCopy];
NSMutableArray *views = [NSMutableArray array];

//Views
for (View *view in selectedProfile.views) {
    NSMutableDictionary *viewDict = [[view dictionaryWithValuesForKeys:[[[view entity] attributesByName] allKeys]] mutableCopy];
    NSMutableArray *controls = [NSMutableArray array];
         //Much more for-loops
    [viewDict setObject:controls forKey:@"controls"];
    [views addObject:viewDict];
}

[profileDict setObject:views forKey:@"views"];

if([profileDict writeToFile:[path stringByStandardizingPath] atomically:YES]) 
    NSLog(@"Saved");
else
    NSLog(@"Not saved");
[profileDict release];
}
但如果想在另一边导入
- (Profile*) importProfileFromPath:(NSString *)path{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
Profile *newProfile = [NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:context];

NSMutableDictionary *profileDict = [NSMutableDictionary dictionaryWithContentsOfFile:[path stringByStandardizingPath]];
[newProfile setValuesForKeysWithDictionary:profileDict];
}
我得到一个例外,这并不会让我感到困惑,因为Profile期望一个NSSet而没有NSArray。
[__NSCFArray intersectsSet:]: unrecognized selector sent to instance 0x4e704c0 *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFArray intersectsSet:]: unrecognized selector sent to instance 0x4e704c0'
所以我有两个问题: 一方面,我无法将NSSet写入File。 另一方面是我的Profile类期望NSSet。 所以我尝试创建一个实现writeToFile的NSSet类别
@implementation NSSet(Persistence)

- (BOOL)writeToFile:(NSString*)path atomically:(BOOL)flag{
    NSMutableArray *temp = [NSMutableArray arrayWithCapacity:self.count];
    for(id element in self)
        [temp addObject:element];
    return [temp writeToFile:path atomically:flag];
}

+ (id)setWithContentsOfFile:(NSString *)aPath{
    return [NSSet setWithArray:[NSArray arrayWithContentsOfFile:aPath]];
}
@end
但我的功能没有被调用。 有没有其他方法来编写我的NSSet或告诉
setValuesForKeysWithDictionary
键“视图”是NSArray? 还是一种简单的方法来导入/导出ManagedObjects?     
已邀请:
嵌套的NSDictonarys出了问题,所以我告别动态的方式。 这是我完整的解决方案,以帮助他人 ViewController调用im / export函数
- (void) exportProfile:(Profile *)profile toPath:(NSString *)path{
    //Call the NSManagedobject function to export
    NSDictionary *profileDict = [self.selectedProfile dictionaryForExport];

    if([profileDict writeToFile:[path stringByStandardizingPath] atomically:YES]) 
        NSLog(@"Saved");
    else
        NSLog(@"Not saved");
}

- (void) importProfileFromPath:(NSString *)path{
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    Profile *newProfile = [NSEntityDescription insertNewObjectForEntityForName:@"Profile" inManagedObjectContext:context];

    //load dictonary from file
    NSMutableDictionary *profileDict = [NSMutableDictionary dictionaryWithContentsOfFile:[path stringByStandardizingPath]];
    //call the NSManagedObjects import function
    [newProfile importWithDictonary:profileDict context:context];

    NSError *error = nil;
    if (![context save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}
NSManagedObject函数我有一个层次结构,所以我把它们放在我的每个NSManagedObjects中
- (void) importWithDictonary:(NSDictionary*)dict context:(NSManagedObjectContext*)context{
    self.name = [dict objectForKey:@"name"];

    //Relationship
    for (NSDictionary *view in [dict objectForKey:@"views"]) {
        View *tempView = [NSEntityDescription insertNewObjectForEntityForName:@"View" inManagedObjectContext:context];
        [tempView importWithDictonary:view context:context];
        tempView.profile = self;
        [self addViewsObject:tempView];
    }
}

- (NSDictionary*) dictionaryForExport{ 
    //propertys
    NSMutableDictionary *dict = [[[self dictionaryWithValuesForKeys:[[[self entity] attributesByName] allKeys]] mutableCopy] autorelease];
    NSURL *objectID = [[self objectID] URIRepresentation];
    [dict setObject: [objectID absoluteString] forKey:@"objectID"];
    NSMutableArray *views = [NSMutableArray array];

    //relationship
    for (View *view in self.views) {
        [views addObject:[view dictionaryForExport]];
    }
    [dict setObject:views forKey:@"views"];
    return dict;
}
不是最美丽的解决方案,但它的工作:) 我仍然要弄清楚如何避免我的n:m关系重复 谢谢     
您可以尝试重写NSManagedObject的setValuesForKeysWithDictionary的默认实现吗? 查看文档,您只需要在子类中实现setValue:forKey: 您应该能够在那里获取NSSet并在抛出异常之前自己处理它? [免责声明 - 我从未这样做过!]     

要回复问题请先登录注册