HELP!我现在已经把这个问题拉出来了1个小时。
我正在我的.h文件中创建一个数组
@interface LeftMenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate>
@property (strong, nonatomic) MKNetworkOperation *myNetworkOperation;
@property (strong, nonatomic) NSArray *listOfClubsArray;
@end
然后我从JSON
中的API调用中提取项目列表///////////////////////////////////////////
#pragma mark Retreive List of Clubs / Teams
///////////////////////////////////////////
- (void)getListOfClubs {
// Because we have a success from login auth we will now make a seperate call to gather club details
self.myNetworkOperation = [app.teamManagerWrapper deviceID:@"adam" userToken:@"sdfdfgf" onCompletion:^(NSDictionary *result) {
NSDictionary *data2 = [result objectForKey:@"data"];
self.listOfClubsArray = [data2 objectForKey:@"user_clubs"];
[self.tableView reloadData];
} onError:^(NSError *error) {
// error none
}];
JSON看起来像这样
{
"code": 200,
"data": {
"user_clubs": {
"5238": {
"club_id": "45484",
"name": "Testing This Club Name",
"sport_id": "7",
"primary_colour": "#000000",
"secondary_colour": "#f15729",
"members": null,
"sections": {
"s": "Mens",
"j": "Junior",
"m": "Summer League",
"l": "Ladies"
}
我的tablecell看起来像这样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ClubCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tempDictionary= [self.listOfClubsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:@"name"];
return cell;
}
它正在崩溃
NSDictionary *tempDictionary= [self.listOfClubsArray objectAtIndex:indexPath.row];
使用此崩溃日志
2013-11-19 12:13:43.069 Manager App[11394:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b0e9d0
2013-11-19 12:13:43.071 Manager App[11394:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8b0e9d0'
*** First throw call stack:
任何帮助都非常感谢!!
答案 0 :(得分:5)
在你的JSON示例中,没有俱乐部数组而是字典,因此objectAtIndex
将不起作用。
{
"code": 200,
"data": {
"user_clubs": {
"5238": {
"club_id": "45484",
"name": "Testing This Club Name",
在这里,您可以看到data / user_clubs拥有一个dictionanry,如果您想要使用它的Testing This Club Name
键来抓住它:
NSDictionary *tempDictionary= [self.listOfClubsArray objectForKey:@"5238"];
现在这不是你想要的,解决方案可能就是这样:
NSArray *keys = [self.listOfClubsArray allKeys];
NSString *key = [keys objectAtIndex:indexPath.row];
NSDictionary *tempDictionary= [self.listOfClubsArray objectForKey:key];
但请确保您也可以通过UITableView来更正密钥数量并更改
@property (strong, nonatomic) NSArray *listOfClubsArray;
到
@property (strong, nonatomic) NSDictionary *listOfClubsArray;
答案 1 :(得分:0)
JSon只返回字典,但是你将数据存储在数组中。那就是为什么会崩溃。你需要改变那样......
尝试使NSMutableDictionary不是数组,因为在json中没有数组。所以你需要改变它
@property (strong, nonatomic) NSMutableDictionary *listOfClubsDic;
答案 2 :(得分:0)
补充同事的回应:
如果有这个选项,您可以使用user_clubs中的数组生成正确的JSON,如下所示:
{
"code": 200,
"data": {
"user_clubs":[ {
"5238": {
"club_id": "45484",
"name": "Testing This Club Name",
"sport_id": "7",
"primary_colour": "#000000",
"secondary_colour": "#f15729",
"members": null,
"sections": {
"s": "Mens",
"j": "Junior",
"m": "Summer League",
"l": "Ladies"
}
....
]
这样你就有了一系列字典,其中的密钥在这种情况下是5238,这对我来说没什么意义(应该对你做),无论如何我的建议是将这个识别符放在代表的对象中并简单地枚举数组中的所有对象(数组已经有序也可以保存几步),如下所示:
像这样:{
"code": 200,
"data": {
"user_clubs":[
{
"user_id" = "5238",
"club_id": "45484",
"name": "Testing This Club Name",
"sport_id": "7",
"primary_colour": "#000000",
"secondary_colour": "#f15729",
"members": null,
"sections": {
"s": "Mens",
"j": "Junior",
"m": "Summer League",
"l": "Ladies"
}
}
....
]
通过这种方式,您可以使用已知密钥的JSON对象(字典)数组,并且可以轻松操作。