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?
没有找到相关结果
已邀请:
2 个回复
亥套惟间连
NSManagedObject函数我有一个层次结构,所以我把它们放在我的每个NSManagedObjects中
不是最美丽的解决方案,但它的工作:) 我仍然要弄清楚如何避免我的n:m关系重复 谢谢
脖呐