我正在努力使我的NSMangedObjectClass配置文件可以导出
我试试this way
如果我在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'
所以我有两个问题:
所以我尝试创建一个实现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?
答案 0 :(得分:2)
嵌套的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关系中避免重复
由于
答案 1 :(得分:1)
您可以尝试重写NSManagedObject的setValuesForKeysWithDictionary的默认实现吗?
查看the documentation您只需要在子类中实现setValue:forKey:吗?
你应该能够在那里获取NSSet并在抛出异常之前自己处理它?</ p>
[免责声明 - 我从未这样做过!]