我正在运行,使我的NSMangedObjectClass配置文件im- /可导出。
我试试this way
如果我在NSArrays中写入关系,导出工作正常,因为NSSet没有实现writeToFile
。NSManagedObject层次结构导入和导出
- (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];
}
我得到一个例外,这并不让我困惑造成档案需要一个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到文件。
- 另一方面是我的Profile类期望NSSet。
所以我试图创建NSSet中的一个类别,它实现将writeToFile
@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?
还是一种简单的方法来im-/export ManagedObjects?
什么是例外? – deanWombourne 2011-04-11 15:15:02
'NSSet'有一个实例方法' - (NSArray *)allObjects'和一个类方法'+(id)setWithArray:(NSArray *)array',可以帮助您。 – Joe 2011-04-11 15:22:29
'[__NSCFArray intersectsSet:]:无法识别的选择发送到实例0x4e704c0 ***终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因:“ - [__ NSCFArray intersectsSet:]:无法识别的选择发送到实例0x4e704c0'' 像我表示Profile需要一个NSSet并且调用NSSet的函数而不是NSArray的方法 – Seega 2011-04-11 15:23:49