paul_1991
paul_1991

Reputation: 231

NSMutable Array Error

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"].

Upvotes: 0

Views: 165

Answers (4)

rckoenes
rckoenes

Reputation: 69469

You should alloc and init the MutableArray before you can use it.

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 = [[NSMutableArray alloc] initWithCapacity:[subs count]];

    for (NSDictionary *iscrizione in subs){
        NSLog(@"title: %@" ,[iscrizione objectForKey:@"title"]);
        NSLog(@"htmlUrl: %@", [iscrizione objectForKey:@"htmlUrl"]);
        list=[NSArray arrayWithObjects:[iscrizione objectForKey:@"title"], [iscrizione objectForKey:@"htmlUrl"], nil];  
        [sub addObject:list];

    }
    // here you should add code to handle the sub array.

    // Do not forget to release the allocated array
    [sub release], sub =nil;
}

Upvotes: 0

alloc_iNit
alloc_iNit

Reputation: 5183

Following will do work properly...

    NSMutableArray *sub = [[NSMutableArray alloc] init];
    for (NSDictionary *iscrizione in subs)
    {
        NSString *title = [NSString stringWithFormat:@"%@", [iscrizione objectForKey:@"title"]];
        NSString *htmlURL = [NSString stringWithFormat:@"%@",[iscrizione objectForKey:@"htmlUrl"]];
        list=[NSArray arrayWithObjects:title, htmlURL, nil];  
        [sub addObject:list mutableCopy];
    }

Upvotes: 0

Praveen S
Praveen S

Reputation: 10393

sub is not allocated memory. You can create a empty array NSMutableArray *sub = [NSMutableArray array]; outside the if condition or have a reference maintained in your code.

Upvotes: 0

Jacob Gorban
Jacob Gorban

Reputation: 1461

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.

Upvotes: 2

Related Questions