【问题标题】:NSDictionary isn't responding to objectForKey and valueForKeyNSDictionary 没有响应 objectForKey 和 valueForKey
【发布时间】:2012-02-08 20:21:41
【问题描述】:

我有以下

// this code is inside cellForRowAtIndexPath for a TableViewController

id answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:@selector(objectForKey)]) {
    cell.textLabel.text = [answer valueForKey:@"answer_id"];
} else {
    // I'm ending up here, instead of the cell.textLabel being set
    [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to valueForKey, class: %@", [answer class]];
}

self.answers 设置为

// the question that gets passed here is a parsed single object 
// from the `/questions` path
- (NSArray *)answersForQuestion:(NSDictionary *)question {
    NSString *contents = [self loadContentsForPath:[question valueForKey:@"question_answers_url"]];
valueForKey:@"question_answers_url"]];
    NSDictionary *data = [contents JSONValue];
    NSArray *answers = [data valueForKey:@"answers"];
    return answers;
}

- (NSString *)loadContentsForPath:(NSString *)path {
    NSString *wholeURL = [@"http://api.stackoverflow.com/1.1" stringByAppendingString:path];    
    return [NSString stringWithContentsOfURL:[NSURL URLWithString:wholeURL] encoding:NSUTF8StringEncoding error:nil];
}

我正在做同样的事情来加载问题,它工作得很好,但似乎 当我尝试做[answers valueForKey:@"answer_id"]时,答案失败。

我认为这不是 JSON 解析器的问题,因为它适用于 /questions 数据。

当调试器在异常上停止并且当我尝试右键单击 -> 在answers 上打印描述时,我得到

Printing description of answer:
<CFBasicHash 0x6ec1ec0 [0x1474b38]>{type = mutable dict, count = 13,
entries =>
    1 : <CFString 0x6ec57d0 [0x1474b38]>{contents = "down_vote_count"} = <CFNumber 0x6e1dc00 [0x1474b38]>{value = +0, type = kCFNumberSInt32Type}
    2 : <CFString 0x6ec4ee0 [0x1474b38]>{contents = "last_activity_date"} = <CFNumber 0x6ec5780 [0x1474b38]>{value = +1326379080, type = kCFNumberSInt64Type}
    3 : <CFString 0x6ec44b0 [0x1474b38]>{contents = "community_owned"} = <CFBoolean 0x1474f68 [0x1474b38]>{value = false}
...

对我来说这似乎是一个常规哈希。我尝试了objectForKeyvalueForKey,但它们都不起作用,即

exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6b2a330'

当我这样做时

cell.textLabel.text = [answer objectForKey:@"answer_id"];

【问题讨论】:

  • 您真的阅读了异常描述吗?它会告诉你问题是什么就在那里
  • @JonathanGrynspan 是的,但我不知道我不能将 NSNumber 分配给 NSString,因为我主要做 Ruby,这完全可以。现在这似乎是一个非常愚蠢的错误。

标签: iphone objective-c ios dictionary nsdictionary


【解决方案1】:

你可以使用:--

NSDictionary *answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:@selector(objectForKey)]) {
    cell.textLabel.text = [answer valueForKey:@"answer_id"];
} else {
    [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to valueForKey, class: %@", [answer class]];
}

 id answer = [self.answers objectAtIndex:indexPath.row];
if (answer isKindOfClass:[NSDictionary class])
{
  cell.textLabel.text = [answer valueForKey:@"answer_id"];
    } else {
        // I'm ending up here, instead of the cell.textLabel being set
        [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to valueForKey, class: %@", [answer class]];
    }

【讨论】:

  • 您提出的解决方案存在几个问题:在您的第一个解决方案中,您正在测试错误的选择器(objectForKeyobjectForKey:),然后您使用错误的选择器来实际获取字典中的对象(valueForKeyobjectForKey:)。你的第二个版本稍微好一点,但仍然使用valueForKey:。在您的两个解决方案中,您都没有将 NSNumber 转换为字符串。
  • 是的 .. 它应该是 objectForKey: 在这两种情况下无处不在。
【解决方案2】:

: 是方法名的一部分,所以你需要这样做:

if ([answer respondsToSelector:@selector(objectForKey:)]) {

然后使用objectForKey:,而不是valueForKey:。第一个是访问字典中的对象,后者是所谓的Key-Value Coding。所以它是:

id answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:@selector(objectForKey:)]) {
    cell.textLabel.text = [answer objectForKey:@"answer_id"];
} else {
    [NSException raise:@"Answer is of invalid class" format:@"It should be able to respond to objectForKey:, class: %@", [answer class]];
}

最后但并非最不重要的一点是,您从 answer 字典中得到的对象似乎是 NSNumber,而不是 NSString。因此,您可能希望将文本的设置更改为:

cell.textLabel.text = [[answer objectForKey:@"answer_id"] description];

【讨论】:

    【解决方案3】:

    其实stackoverflow的api会发回一个数字。也就是说answer_id键存储的值是字典中的NSNumber而不是NSString,你应该相应地对待它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-16
      • 2010-11-06
      • 2016-10-26
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多