gpt4 book ai didi

iphone - NSManagedObject 层次结构导入和导出

转载 作者:行者123 更新时间:2023-12-03 19:15:50 31 4
gpt4 key购买 nike

我正在努力使我的 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',<br/>
reason: '-[__NSCFArray intersectsSet:]: unrecognized selector sent to instance 0x4e704c0'

所以我有两个问题:

  • 一方面,我无法将 NSSet 写入文件。
  • 另一边是我的 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关键的“ View ”是一个 NSArray?

或者导入/导出 ManagedObjects 的简单方法?

最佳答案

嵌套的 NSDictonary 出现问题,所以我告别了动态方式。这是我帮助他人的完整解决方案
ViewController 调用导入/导出函数

- (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 关系中出现重复

谢谢

关于iphone - NSManagedObject 层次结构导入和导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5623384/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com