0

Hi I'm studying a 9TH lesson of iTunesU CS193P about table view and compiler report me this error NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'

my simulator is iPad 6.1

so I have

a class called GraphViewController.m

#define FAVORITE_KEY @"GraphViewController.Favorite"
- (IBAction)addFavorite:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *favorites = [[defaults objectForKey:FAVORITE_KEY] mutableCopy];
    if (!favorites) favorites = [NSMutableArray array];
    [favorites addObject:self.program];
   // NSLog(@"contenuto favorites %@",favorites);
    [defaults setObject:favorites forKey:FAVORITE_KEY];
    [defaults synchronize];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"GraphTableView"]) {
        NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
        [segue.destinationViewController setPrograms:program];
    }
}

(setPrograms is the setter where i have the data to send at my tableviewcontroller called CalculatorTVC.m)

a class called CalculatorTVC.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.programs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cellTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    id program = [self.programs objectAtIndex:indexPath.row];

    cell.textLabel.text = [@"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
    NSLog(@"contenuto di program %@",program);  

    return cell;
}

(programs is a public property where I put the data from GraphViewController.m)

In my storyboard I have a split view ...in MasterViewController i have toolbar with bar button item wired with table view controller (CalculatorTVC.m) in popover style identifier is GraphTableView and i have a round rect button that is addFavorite describe here up

the error is * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object' * First throw call stack: (0x1ca1012 0x10dee7e 0x1ca0deb 0x1d21c4f 0x1d21911 0x4a5b 0x8f65 0xdd8fb 0xdd9cf 0xc61bb 0xd6b4b 0x732dd 0x10f26b0 0x229dfc0 0x229233c 0x22a0238 0x6b6e3 0x4f1476 0x870989a 0x4f2555 0x489ef9 0x46ab99 0x46ac14 0x10f2705 0x262c0 0x262a64 0x10f2705 0x262c0 0x26258 0xe7021 0xe757f 0xe66e8 0x55cef 0x55f02 0x33d4a 0x25698 0x1bfcdf9 0x1bfcad0 0x1c16bf5 0x1c16962 0x1c47bb6 0x1c46f44 0x1c46e1b 0x1bfb7e3 0x1bfb668 0x22ffc 0x24bd 0x23e5) libc++abi.dylib: terminate called throwing an exception (lldb)

please help me

thanks for your patience regards


ok I find where it crashes....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cellTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    id program = [self.programs objectAtIndex:indexPath.row];

    cell.textLabel.text = [@"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];
    NSLog(@"contenuto di program %@",program);  

    return cell;
}

the crash in the line

cell.textLabel.text = [@"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]];

if I put NSLOG before this line i see in the output NSLOG result...but if I put NSLOG after this I don't see anything in the output

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
walle
  • 33
  • 1
  • 5

1 Answers1

0
 NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
[segue.destinationViewController setPrograms:program];

Here you're passing an immutable array to the destination view controller. If it is expecting a mutable array and tries to modify it, you'll get that crash. You need mutableCopy here as well.

If the property should be a mutable array, you should get compiler warnings. Don't ignore these!

You're crashing on remove, not add, by the way, so you haven't included the right code in your question. Enable exception breakpoints to find the line you're crashing on.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • ok I find where it crashes....- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cellTable"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... id program = [self.programs objectAtIndex:indexPath.row]; cell.textLabel.text = [@"y = " stringByAppendingString:[CalculatorBrain descriptionProgram:program]]; NSLog(@"contenuto di program %@",program); return cell; } – walle Mar 08 '13 at 18:36