gpt4 book ai didi

iphone - NSDictionary 没有响应 objectForKey 和 valueForKey

转载 作者:行者123 更新时间:2023-11-28 18:26:08 28 4
gpt4 key购买 nike

我有以下内容

// 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 数据。

当调试器因异常而停止时,当我尝试右键单击 -> Print Description on 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"];

最佳答案

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

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

关于iphone - NSDictionary 没有响应 objectForKey 和 valueForKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8836881/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com