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!
[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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With