我正在尝试将图像添加到核心数据中并在需要时加载它。我目前正在将 NSImage 添加到核心数据中,如下所示:
Thumbnail *testEntity = (Thumbnail *)[NSEntityDescription insertNewObjectForEntityForName:@"Thumbnail" inManagedObjectContext:self.managedObjectContext];
NSImage *image = rangeImageView.image;
testEntity.fileName = @"test";
testEntity.image = image;
NSError *error;
[self.managedObjectContext save:&error];
Thumbnail 是实体名称,我在 Thumbnail 实体下有两个属性 - 文件名(NSString)和图像(id - 可转换)。
我正在尝试按如下方式检索它们:
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *imageEntity = [NSEntityDescription entityForName:@"Thumbnail" inManagedObjectContext:[context valueForKey:@"image"]];
[fetchRequest setEntity:imageEntity];
NSError *error;
NSArray * array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (array == nil) {
NSLog(@"Testing: No results found");
}else {
_coreDataImageView.image = [array objectAtIndex:0];
}
我最终得到这个错误:
[<NSManagedObjectContext 0x103979f60> valueForUndefinedKey:]: this class is not key value coding-compliant for the key image.
图像已添加但无法检索。
关于如何进行此操作的任何想法?我做对了吗?