【问题标题】:NSDictionary objectAtIndex causing an unrecognized selector exceptionNSDictionary objectAtIndex 导致无法识别的选择器异常
【发布时间】:2013-02-20 22:03:45
【问题描述】:

我在名为 favToLoad 的 NSMutableDictionary 中有数组。我正在尝试使用 UITableView 的 indexPath.row 逐一获取数组。

代码:

NSArray* fav = [[NSArray alloc] init];
NSLog(@"IndexPath = %d", indexPath.row);
fav = [favToLoad objectAtIndex:indexPath.row];
NSLog(@"Fav = %@", fav);

错误:

[NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance

解决方案

我已经通过使用 NSMutableArray 而不是 NSMutableDictionary 解决了我的问题

【问题讨论】:

  • 由于错误状态,您正在向 NSDictionary 发送 objectAtIndex: 消息,而 NSDictionary 不知道如何回答。你应该使用objectForKey:
  • 我没有“objectForKey”,它不建议它:(
  • 把这个日志放进去:NSLog(@"%@",[favToLoad class]) 它显示了什么?
  • 我得到了这个:2013-02-20 23:17:48.014 PEBKAC[2232:907] __NSCFDictionary 2013-02-20 23:17:48.015 PEBKAC[2232:907] IndexPath = 0 2013- 02-20 23:17:48.016 PEBKAC[2232:907] -[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x1f0a0b80`
  • 我们知道这是一本字典,错误信息很清楚favToLoadNSCFDictionaryfavToLoad 来自哪里? Xcode 不建议 objectForKey,因为在您输入的上下文中,它可能是id 指针,或者无法以其他方式推断。显示favToLoad的声明和赋值,答案可能就在那里。

标签: objective-c object nsarray nsmutabledictionary


【解决方案1】:

字典没有索引。你不能这样做。相反,您需要使用 -objectForKey: 和适当的键来获取您想要的值(您可以在调试器中使用 'po favToLoad' 来检查内容)。

【讨论】:

  • 我没有“objectForKey”,它不建议它:(
  • Armand:那么请在您的帖子中包含“favToLoad”的声明。很可能不是你想的那样。
  • Swift 2.0:这是您访问ObjectForKey = inArray[FindObject]的方式
【解决方案2】:

您应该使用objectForKey: 而不是objectAtIndex:

objectAtIndex:NSArray 类有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 2018-02-15
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多