Я пытаюсь сделать свой профиль NSMangedObjectClass не- / экспортируемым.
Я пробую таким образом
Экспорт работает правильно, если я пишу отношения в 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'
Итак, у меня две проблемы:
- С одной стороны, я не могу записать 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?
Или простой способ импорта / экспорта управляемых объектов?
NSSet
имеет метод экземпляра- (NSArray *)allObjects
и метод класса+ (id)setWithArray:(NSArray *)array
, которые могут вам помочь. - person Joe   schedule 11.04.2011[__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, а не методы NSArray - person Seega   schedule 11.04.2011