Search code examples
objective-carraysjsonparsingnsmutablearray

Remove object from NSMutableArray after parsing JSON


The JSON source formatted with jsonviewer.stack.hu:

Json formatter

My Parse Method (simplified):

- (void)parseMethod {

    // OTHER STUFF

    arrayList = [[NSMutableArray alloc] init];    

    NSURL *url2 = // THE URL SOURCE OF JSON OBJECT, ON A REMOTE SERVER

    NSURLRequest *request2 = [NSURLRequest requestWithURL:url2 cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0];

    AFJSONRequestOperation *operation2 = [AFJSONRequestOperation
                                         JSONRequestOperationWithRequest:request2
                                         success:^(NSURLRequest *request2, NSHTTPURLResponse *response2, id JSON)
                                         {
                                             arrayList = [JSON objectForKey:@"list"];

                                             // HERE I TRIED TO WRITE [arrayListPrev removeObjectAtIndex:0];

                                             NSMutableArray *arrayList1 = [arrayList valueForKey:@"list1"];
                                         }
                                         failure:^(NSURLRequest *request2, NSHTTPURLResponse *response2, NSError *error2, id JSON2) { 
                                         }];
    [operation2 start];

}

The problem: After the parsing I want to remove the FIRST OBJECT of array named "list", because I must populate the rows of a UITableView with all the values of list1 in the arrays EXCEPT the first ( list array->array number 0->list1 value of 0 ). I have tried the code:

[arrayList removeObjectAtIndex:0];

In several position but app crashes with error:'-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'... so what's the best way to REMOVE the FIRST object (array number 0) from that NSMutableArray *list AFTER the parsing, to ELIMINATE The list1 value of 0 object? Thanks!


Solution

  • In spite of you write arrayList = [[NSMutableArray alloc] init];

    Here, as i guess, you assign simple immutable array instance

    arrayList = [JSON objectForKey:@"list"];

    You can do this instead:

    arrayList = [[JSON objectForKey:@"list"] mutableCopy];
    [arrayList removeObjectAtIndex:0];