I’ve this code
SBJsonParser *vol= [[SBJsonParser alloc]init];
if (body) {
NSArray *feeds = [vol objectWithString:body error:nil];
NSDictionary *results = [body JSONValue];
NSArray *subs = [results valueForKey:@"subscriptions"];
NSArray *list;
NSMutableArray *sub;
for (NSDictionary *iscrizione in subs){
NSLog([iscrizione objectForKey:@"title"]);
NSLog([iscrizione objectForKey:@"htmlUrl"]);
list=[NSArray arrayWithObjects:[iscrizione objectForKey:@"title"], [iscrizione objectForKey:@"htmlUrl"], nil];
[sub addObject:list];
}
But when I try to add list to the NSMutableArray the program crashes. Why?
How can I insert [iscrizione objectForKey:@"title"] and [iscrizione objectForKey:@"htmlUrl"]
in a data structure?
EDIT**
SBJsonParser *vol= [[SBJsonParser alloc]init];
if (body) {
NSArray *feeds = [vol objectWithString:body error:nil];
NSDictionary *results = [body JSONValue];
NSArray *subs = [results valueForKey:@"subscriptions"];
NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];
for (NSDictionary *iscrizione in subs){
[sub addObject:iscrizione];
}
NSDictionary *it=[[NSDictionary alloc]initWithDictionary:[sub objectAtIndex:0]];
NSLog([it objectForKey:@"title"]);
}
This is my code. But I can’t understand why I cannot NSLog [it objectForKey:@”title”].
You didn’t initialize your NSMutableArray.
Should be
NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];Technically, you could simply allocate it with
NSMutableArray *sub = [[NSMutableArray array];as well but this could be more efficient, as you already know how many data entries there will be.
Just pay attention to memory management, you need to release it in the first case and it’s autoreleased in the second case.