NSMangedObjectClass プロファイルを im-/exportable にしようとしています。
NSSet が実装されていないため、NSArrays に Relationships を記述すると、この方法でエクスポートが正しく機能
し ます。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 を想定していないため、混乱することはありません。
だから私は2つの問題があります:
[__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 をファイルに書き込めません。
- 反対側には、NSSet を期待する Profile クラスがあります。
そこで、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 をインポート/エクスポートする簡単な方法はありますか?