I am at startup to make my NSMangedObjectClass profile im- / exportable.
I will try this way
Exporting works correctly if I write relationships in NSArrays because NSSet does not have 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]; }
But if you want to import on the other hand
- (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]; }
I get an exception, this does not confuse me, because Profile expects NSSet and 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'
Therefore, I have two problems:
- For one thing, I cannot write NSSet for a file.
- On the other hand, my profile class expects NSSet.
So, I tried to create an NSSet category that implements 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
But my functions are not called.
Is there any other way to write my NSSet or tell setValuesForKeysWithDictionary "Views" Key is an NSArray?
Or an easy way to import or export ManagedObjects?
Seega source share