-1

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;

            );
        }
    );
}
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51
  • `(` marks the begin of an NSArray. If you look at the original JSON that would be a `[` character. You need to index an array, not access it by key. – Hot Licks Jan 27 '15 at 19:25
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Jan 27 '15 at 19:25
  • @HotLicks I'm sorry I get confused may I ask now I've changed the `_dicSubCategory ` to `_arraySubCategory` (array). Could you please explain more? – Luai Kalkatawi Jan 27 '15 at 19:42
  • `_dicSubCategory` is not a dictionary. The type of the variable is (mostly) irrelevant -- what's important is the type of the object the variable addresses, and that is an array. It's an array because the value of "sub_category" is an array (as can be seen by the `(` character). – Hot Licks Jan 27 '15 at 19:54
  • If you have a box labeled "dog" and put a cat in it, the cat does not turn into a dog. – Hot Licks Jan 27 '15 at 19:56
  • @HotLicks Finally It is started to work. I'm only getting an error ` this class is not key value coding-compliant for the key name.` when the value is equal to zero for example "sub_category" = 0; in `Service` & `Collections`. How I can fix it please? – Luai Kalkatawi Jan 27 '15 at 21:09
  • That's essentially the same error as "unrecognized selector", only different. The zero value comes through as an NSNumber, and you must test the type of the value coming back from fetching "sub_category" to see whether it's an NSNumber or an NSArray. You can test the type with, eg `if ([theSubCategoryValue isKindOfClass: [NSNumber class]])`. – Hot Licks Jan 27 '15 at 21:25
  • @HotLicks can you please write your answer? So I accept the question. – Luai Kalkatawi Jan 30 '15 at 16:49

1 Answers1

0

( marks the beginning of an NSArray. If you look at the original JSON that would be a [ character. You need to index an array, not access it by key.

This means that _dicSubCategory is not a dictionary, regardless of how the variable is declared. The type of the variable is (mostly) irrelevant -- what's important is the type of the object the variable addresses, and that is an array. It's an array because the value of "sub_category" is an array (as can be seen by the ( character).

If you have a box labeled "dog" and put a cat in it, the cat does not turn into a dog.

this class is not key value coding-compliant for the key name.

That's essentially the same error as "unrecognized selector", only different. The zero value for "sub_category" comes through as an NSNumber, and you must test the type of the value coming back from fetching "sub_category" to see whether it's an NSNumber or an NSArray. You can test the type with, eg:

if ([theSubCategoryValue isKindOfClass:[NSNumber class]])
Hot Licks
  • 47,103
  • 17
  • 93
  • 151