因未捕获的异常终止应用程序' NSUnknownKeyException'此类不是键值.img的键值编码兼容

时间:2013-06-10 12:44:28

标签: ios objective-c nsmutabledictionary

尝试异步加载图像我收到此错误 “因未捕获的异常而终止应用程序'NSUnknownKeyException',原因:'[valueForUndefinedKey:]:此类不符合键值编码关键img。'“ 我的代码如下

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

请告知我在哪里出错了。 谢谢 MAYUR

4 个答案:

答案 0 :(得分:1)

替换

[record valueForKey:@"img"]

[record objectForKey:@"img"]

答案 1 :(得分:1)

 Check if object in array is of dictionary type

  NSString *str = "URL of an image";

    NSMutableDictionary *record = [NSMutableDictionary dictionary];

if([[_array objectAtIndex:indexPath.row]isKindOfClass:[NSDictionary class]])
    record = [_array objectAtIndex:indexPath.row];
else
      [record setObject:str forKey:@"img"];


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

答案 2 :(得分:0)

这条线是唯一可能的

[记录valueForKey:@“img”]

如果未设置密钥img,

objectForKey:将返回nil。在你的情况下更好的选择。

但是,请查看您的代码。首先,您可以创建新的NSMutableDictionary并将其分配给record。然后将字符串对象添加到该字典中。好到目前为止。 之后,您将从分配给record的数组_array中获取一个对象。到那时你再也没有提到新创建的dicitionary了。这是有意的。如果您不使用ARC,则在此处创建内存泄漏。

编辑以回复以下评论:

NSMutableDictionary *record = [NSMutableDictionary dictionary]; // here you create a new empty instance of NSMutableDictionary and store it in record. It has a retain count of 1
[record setObject:str forKey:@"img"]; // here you add an object to that new dictionary. Now it has exactly one entry with the key "img"
record = [_array objectAtIndex:indexPath.row]; //and here you discard the retained newly creaded dictionary by assigning another one from the array _array. What is the purpose of that?

答案 3 :(得分:0)

借助您的评论和答案解决了问题。我所做的是在全局使用的类中创建NSMutable Dictionary并初始化变量。谢谢大家的帮助

arr = [[NSMutableDictionary alloc] init];


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    if ([[[DataManager sharedInstance] arr] objectForKey:str]) {
        NSLog(@"in if");

         [cell m_img].image = [UIImage imageWithData:[[[DataManager sharedInstance] arr] objectForKey:str]]; 
    } else {
        dispatch_async(queue, ^{
             NSLog(@"in else");



                NSURL  *imageurl = [[NSURL alloc] initWithString:str];
                NSData *image = [[NSData alloc] initWithContentsOfURL:imageurl];
                [[[DataManager sharedInstance] arr] setObject :image forKey:str];
                [cell m_img].image = [UIImage imageWithData:[[[DataManager sharedInstance] arr] objectForKey:str]]; 

        });
    }