【问题标题】:Terminating app due to uncaught exception 'NSUnknownKeyException' this class is not key value coding-compliant for the key .img由于未捕获的异常“NSUnknownKeyException”而终止应用程序此类不符合键 .img 的键值编码
【发布时间】:2013-06-10 12:44:27
【问题描述】:

尝试异步加载图像我收到此错误 “由于未捕获的异常 '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];
            });
        });
    }

请告知我在哪里出错了。 谢谢 马尤尔

【问题讨论】:

  • 这一行发生了什么:record = [_array objectAtIndex:indexPath.row];?此时,变量record 持有对来自_array 的某个对象的引用,并且您不再对新创建的NSMutableArray 有任何引用。
  • @Theo 实际上我确实有该数组的参考..
  • 对不起,我的意思是你的NSMutableDictionary 没有引用了。

标签: ios objective-c nsmutabledictionary


【解决方案1】:

替换

[record valueForKey:@"img"]

[record objectForKey:@"img"]

【讨论】:

  • NSDictionary 支持 KVC,因此不需要建议的更改。
  • 当我这样做时它仍然崩溃,因为这个错误无法识别选择器发送到实例 0xe050c30'
  • 再次替换 [record setValue:[UIImage imageWithData:image] forKey:@"img"]; with [record setObject:[UIImage imageWithData:image] forKey:@"img"];
  • @Adithya,我认为这不是解决方案。应用程序崩溃是因为 record 不是对 NSMutableDictionary 的引用。 MayurCM,尝试使用调试器或 NSLog(@"%@", [record description]); 来检查 record
【解决方案2】:
 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];
            });
        });
    }

【讨论】:

    【解决方案3】:

    这条线很可能

    [记录valueForKey:@"img"]

    objectForKey: 将在未设置键 img 的情况下返回 nil。在您的情况下是更好的选择。

    但是,请总体检查您的代码。您首先创建一个新的NSMutableDictionary 并将其分配给record。然后将一个字符串对象添加到该字典中。到目前为止还好。 之后,您也从分配给 record 的数组 _array 中获取一个对象。到那时,您不再对新创建的字典有任何引用。是不是故意的。如果您不使用 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?
    

    【讨论】:

    • 是的,实际上我没有使用 ARC 并且它从上面引用。
    • 查看我编辑的答案。它与您的原始问题无关,但您确实存在内存泄漏。
    【解决方案4】:

    在您的 cmets 和答案的帮助下解决了问题。我所做的是在 Globally used 类中创建 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]]; 
    
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2013-06-17
      • 2012-01-04
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多