Trying to load image asynchronously I'm getting this error "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key img.'" My code is as below
NSString *str = "URL of an image";
NSMutableDictionary *record = [NSMutableDictionary dictionary];
[record setObject:str forKey:@"img"];
record = [_array objectAtIndex:indexPath.row];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
if ([record valueForKey:@"img"]) {
NSLog(@"in if");
cell.m_img.image = [record valueForKey:@"img"];
} else {
dispatch_async(queue, ^{
NSLog(@"in else");
NSURL *imageurl = [[NSURL alloc] initWithString:str];
NSData *image = [[NSData alloc] initWithContentsOfURL:imageurl];
dispatch_async(dispatch_get_main_queue(), ^{
[record setValue:[UIImage imageWithData:image] forKey:@"img"];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
});
}
Please advise where am I getting wrong here. Thanks Mayur
record = [_array objectAtIndex:indexPath.row];? At this time, the variablerecordholds a reference to some object from_arrayand you don't have any reference to your newly createdNSMutableArrayanymore. - TheoNSMutableDictionaryanymore. - Theo