I'm retreving JSON data and I've some categories name with sub_category and other without =0
. I can get the categories name and it is working fine but I couldn't get the name
in the sub_category
, It is getting crash when I call _arraySubCategory
Error: reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance
Am I calling the method right? Where would be my issue?
- (IBAction)getData:(id)sender {
NSString *string = BaseURLString;
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
_dic = (NSDictionary *)responseObject;
NSLog(@"Data %@", _dic);
for (NSDictionary *dict in [_dic objectForKey:@"categories"]) {
[_arrayName addObject:[dict objectForKey:@"name"]];
_dicSubCategory = [dict objectForKey:@"sub_category"];
[_arraySubCategory addObject:[_dicSubCategory objectForKey:@"name"]]; //Crashing
NSLog(@"%@", _dicSubCategory);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog("No Connection")
}];
[operation start];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_arrayName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", [_arrayName objectAtIndex:indexPath.row]];
return cell;
}
JSON Data:
{
categories = (
{
name = "Shop By Room";
"sub_category" = (
{
name = "BEDROOM";
"sub_category" = 0;
},
{
name = "LIVING ROOM";
"sub_category" = 0;
}
);
},
{
name = "Drinkware";
"sub_category" = (
{
name = "Glass1";
"sub_category" = 0;
},
{
name = "Glass2";
"sub_category" = 0;
},
{
name = "Service";
"sub_category" = 0;
},
{
name = "Design";
"sub_category" = (
{
name = "RA";
"sub_category" = 0;
},
{
name = "SW";
"sub_category" = 0;
}
);
},
{
name = "Collections";
"sub_category" = 0;
);
}
);
}