Ada*_*ush -3 arrays crash objective-c nsdictionary ios
救命!我已经用这个问题把头发拉了1个小时了.
我正在我的.h文件中创建一个数组
@interface LeftMenuViewController : UIViewController <UITableViewDataSource, UITabBarControllerDelegate>
@property (strong, nonatomic) MKNetworkOperation *myNetworkOperation;
@property (strong, nonatomic) NSArray *listOfClubsArray;
@end
Run Code Online (Sandbox Code Playgroud)
然后我从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
}];
Run Code Online (Sandbox Code Playgroud)
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"
}
Run Code Online (Sandbox Code Playgroud)
我的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;
}
Run Code Online (Sandbox Code Playgroud)
它在这条线上崩溃了
NSDictionary *tempDictionary= [self.listOfClubsArray objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
有了这个崩溃日志
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:
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢!!
在你的JSON示例中,没有俱乐部数组而是字典,因此objectAtIndex不起作用.
{
"code": 200,
"data": {
"user_clubs": {
"5238": {
"club_id": "45484",
"name": "Testing This Club Name",
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到data/user_clubs拥有一个dictionanry,如果你想让Testing This Club Name你用它来获取它:
NSDictionary *tempDictionary= [self.listOfClubsArray objectForKey:@"5238"];
Run Code Online (Sandbox Code Playgroud)
现在这不是你想要的,解决方案可能是这样的:
NSArray *keys = [self.listOfClubsArray allKeys];
NSString *key = [keys objectAtIndex:indexPath.row];
NSDictionary *tempDictionary= [self.listOfClubsArray objectForKey:key];
Run Code Online (Sandbox Code Playgroud)
但请确保您还要通过UITableView来更正密钥数量并进行更改
@property (strong, nonatomic) NSArray *listOfClubsArray;
Run Code Online (Sandbox Code Playgroud)
至
@property (strong, nonatomic) NSDictionary *listOfClubsArray;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1800 次 |
| 最近记录: |