【问题标题】:Saving array of images in core data as transformable将核心数据中的图像数组保存为可转换的
【发布时间】:2015-04-17 06:51:39
【问题描述】:

我想将我的 imageArray 添加到 coredata 中作为可转换的,但这不能正确存储。

我的保存按钮编码。

  - (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”是我的图像数组。

当我要获取这个图像数组时,这个显示为 nil

- (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.
}

这段代码我做错了什么。谢谢。

【问题讨论】:

  • 你想对属性描述做什么?阵列在哪里配置?你看到什么错误/结果?为什么要将图像存储在核心数据中?
  • 这是关于图像数组的描述。错误不过是在我要获取时显示为零。我想要将来从照片库中选择的图像。如果有任何其他方法可以将图像数组存储在核心数据中,请发布。
  • 如果它们在照片库中,为什么不存储资产 URL?

标签: core-data ios6.1


【解决方案1】:

在您的保存代码中:

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];

我什至无法弄清楚这应该做什么。这是完全错误的。你永远不应该分配NSAttributeDescription 的实例,除非你正在构建一个核心数据模型——你没有这样做,而且几乎没有人这样做。创建新条目就可以了。其余的,我不知道。您说imagesForShow 是您的图像数组,所以我不知道您为什么还要在数组中添加属性描述。

在更一般的意义上,如果 newEntry 有一个名为photos 的可转换属性并且 imagesForShowNSArrayUIImage 对象,那么你可以这样做:

[newEntry setValue:imagesForShow forKey:@"photos"];

这类似于您已注释掉的行,但不清楚为什么它被注释掉了。

但无论你做什么,都要摆脱创建 NSAttributeDescription 的代码。

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 2012-05-05
    • 1970-01-01
    • 2020-10-09
    • 2017-07-17
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多