【发布时间】: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`
-
我们知道这是一本字典,错误信息很清楚
favToLoad是NSCFDictionary。favToLoad来自哪里? Xcode 不建议objectForKey,因为在您输入的上下文中,它可能是id指针,或者无法以其他方式推断。显示favToLoad的声明和赋值,答案可能就在那里。
标签: objective-c object nsarray nsmutabledictionary