Core data 将核心数据中的图像阵列保存为可转换的
我想将imageArray作为可转换的添加到coredata中,但存储不正确 我的保存按钮编码Core data 将核心数据中的图像阵列保存为可转换的,core-data,ios6.1,Core Data,Ios6.1,我想将imageArray作为可转换的添加到coredata中,但存储不正确 我的保存按钮编码 - (IBAction)saveButton:(id)sender { NSManagedObjectContext *context = [self managedObjectContext]; NSManagedObject *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"FoodInfo" inManag
- (IBAction)saveButton:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"FoodInfo" inManagedObjectContext:context];
NSAttributeDescription *messageType = [[NSAttributeDescription alloc] init];
[messageType setName:@"photos"];
[messageType setAttributeType:NSTransformableAttributeType];
[imagesForShow addObject:messageType];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unable to save context for class" );
} else {
NSLog(@"saved all records!");
[context save:nil];
}
//[newEntry setValue:imagesForShow forKey:@"images"];
}
这里的“imagesForShow”是我的图像数组。
当我要取这个图像数组时,它显示为零
- (void)viewDidLoad {
[super viewDidLoad];
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"FoodInfo"];
[request setReturnsObjectsAsFaults:NO];
arrayForPhotos = [[NSMutableArray alloc]initWithArray:[context executeFetchRequest:request error:nil]];
// Do any additional setup after loading the view.
}
我对这段代码的错误之处。谢谢。在您的保存代码中:
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newEntry = [NSEntityDescription
insertNewObjectForEntityForName:@"FoodInfo"
inManagedObjectContext:context];
NSAttributeDescription *messageType = [[NSAttributeDescription alloc] init];
[messageType setName:@"photos"];
[messageType setAttributeType:NSTransformableAttributeType];
[imagesForShow addObject:messageType];
我甚至不知道这是怎么回事。这是完全错误的。您永远不应该分配NSAttributedDescription的实例,除非您正在动态构建核心数据模型——您没有这样做,而且几乎没有人会这样做。创建新条目是可以的。其余的,我不知道。你说过imagesForShow是你的图像数组,所以我不知道你为什么还要在数组中添加属性描述
在更一般的意义上,如果newEntry具有名为photos和imagesForShow的可转换属性是UIImage对象的NSArray,则可以执行以下操作:
[newEntry setValue:imagesForShow forKey:@"photos"];
这类似于您注释掉的一行,但不清楚为什么要注释掉它
但是无论您做什么,都要摆脱创建NSAttributedDescription的代码。您想对属性描述做什么?阵列在哪里配置?你看到了什么错误/结果?为什么要将图像存储在核心数据中?这是关于图像阵列的描述。错误是什么,但这是显示零时,我将要获取。我希望在未来的图像,我选择从照片库。如果有任何其他方法可以将图像数组存储在核心数据中,请发布。如果它们在照片库中,为什么不存储资产URL?