0

I have a static NSMutableArray, and this array is the data source of my table view. Then I delete a cell from my table view and it crashes.

here is where I create the array:

static NSMutableArray *listaPiscinas;

my code:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [listaPiscinas removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        [TratamentoView setListaPiscinas:listaPiscinas];
        NSString *URLdeletarPiscina = [[NSString alloc] initWithFormat:@"%@app/excluir_piscina.php?cod_cliente_piscina=%@", URL_CADASTRO, [[listaPiscinas objectAtIndex:indexPath.row] objectForKey:@"cod_cliente_piscina"]];
        dataReciever = [[AsyncDownloader alloc] initWithGetAdress:URLdeletarPiscina delegate:self];

        [tableView reloadData];
    }
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178

2 Answers2

0

While your variable is typed as NSMutableArray, the object assigned to it is a NSArray, that can't be altered.
To further help you show us the code that creates listaPiscinas.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • there are no static method in objective-c. – vikingosegundo Jan 17 '14 at 21:37
  • 1
    @GabrielMolter: you don't waste much time reading documentation, right? – vikingosegundo Jan 20 '14 at 00:35
  • 1
    You should have an proper understanding of [classes in objC](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/DefiningClasses/DefiningClasses.html#//apple_ref/doc/uid/TP40011210-CH3-SW2) and [check this for a short explanation why those class method aren't static](http://stackoverflow.com/questions/8089186/whats-the-difference-between-class-method-and-static-method). Your friend shouldn't be your only source of your objC skills. – vikingosegundo Jan 20 '14 at 01:53
0

To be clear, the first line you posted:

static NSMutableArray *listaPiscinas;

Is where you DECLARE your array, not where you create it.

That line defines a pointer variable that can point to an object. It does not create an object however.

You have not posted the code that creates the array.

If you have code like this:

listaPiscinas = [NSArray arrayWithObjects: @"one", @"two", @"three", nil];

Then you are creating an immutable array and saving a pointer to that immutable array object into a pointer variable that is supposed to point to a mutable array. The compiler will give you a warning, but if you ignore the warning, then your listaPiscinas will point to an immutable array and trying to modify the array will crash your program just like the error you are reporting.

Post the code that creates an object and saves it to your listaPiscinas static variable.

By the way, static variables are usually a bad idea. There are only a few cases where you should be using them. More than likely you should be using an instance variable or property instead.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • it comes from an other NSMutableArray from another class, that gets the content to listaPiscinas using a static method:+ (void)setListaPiscinas:(NSMutableArray*)lista{ listaPiscinas = lista; } –  Jan 17 '14 at 19:31
  • 1
    Except clearly you are wrong and the object you are assigning is not a mutable array. If we can't see that code, and the code in the other class that creates the array, we can't help you get to the bottom of your problem. The expression "The devil is in the details" applies here. – Duncan C Jan 17 '14 at 19:33
  • I think this solved the problem: NSMutableArray *listaTemp = [listaPiscinas mutableCopy]; [listaTemp removeObjectAtIndex:indexPath.row]; listaPiscinas = listaTemp; –  Jan 17 '14 at 19:45
  • Sure, converting the incoming array by making a mutable copy is a valid solution to your problem. – Duncan C Jan 17 '14 at 20:25