Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS when trying to create a new NSDictionary

I'm having troubles with creating NSDictionaries in a loop and adding it to an NSMutableArray.

Basically I just want to change the names of the keys, but since I couldn't find a function for that I went for following code:

- (NSMutableArray *)getCategoriesForChannel:(int)channelId {
    NSDictionary *data = [self call:@"get_categories.ashx"];
    NSArray *categories = [data objectForKey:@"categories"];
    NSMutableArray *returnArray = [NSMutableArray
                          arrayWithCapacity:[categories count]];

    for(NSDictionary *category in categories) {
        [returnArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                         [category objectForKey:@"Channel_id"], @"id",
                         [category objectForKey:@"Channel_name"], "@name", nil]];
    }
    return returnArray;
}

But app always quits when it reaches the addObject: method and throws an EXC_BAD_ACCESS. I think that it has got something to do with memory management, but since I don't really have a C-background I have no idea how to solve this issue. Can someone please point me to the right direction? Thanks in advance!

like image 636
Lucas G Avatar asked Dec 11 '10 17:12

Lucas G


1 Answers

[returnArray addObject:
    [NSDictionary dictionaryWithObjectsAndKeys:
    [category objectForKey:@"Channel_id"], @"id",
    [category objectForKey:@"Channel_name"], "@name", nil]];

If this is in fact the code you have (and the typo wasn't introduced while writing it in your web browser), notice that the last key you have is "@name" instead of @"name". That would effectively be a C-string, rather than an NSString, which can't properly be added into an NSArray (or most collection classes, for that matter).

like image 62
NSGod Avatar answered Sep 22 '22 00:09

NSGod