0

嗨,我正在学习关于表格视图的 iTunesU CS193P 第 9 课,编译器向我报告此错误 NSInternalInconsistencyException',原因:'-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'

我的模拟器是 iPad 6.1

所以我有

一个名为 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 是设置器,我有数据要在我的 tableviewcontroller 上发送,称为 CalculatorTVC.m)

一个名为 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 是一个公共属性,我将 GraphViewController.m 中的数据放入其中)

在我的故事板中,我有一个拆分视图...在 MasterViewController 中,我有带有条形按钮项目的工具栏,它与弹出框样式标识符中的表格视图控制器(CalculatorTVC.m)相连

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)

请帮我

感谢您的耐心问候


好的,我找到它崩溃的地方....

- (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;
}

线上的崩溃

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

如果我将 NSLOG 放在此行之前,我会在输出 NSLOG 结果中看到...但是如果我将 NSLOG 放在此之后,我在输出中看不到任何内容

4

1 回答 1

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

在这里,您将一个不可变数组传递给目标视图控制器。如果它期望一个可变数组并尝试修改它,那么您将遇到崩溃。你在这里也需要 mutableCopy 。

如果属性应该是一个可变数组,你应该得到编译器警告。不要忽视这些!

顺便说一句,你在删除而不是添加时崩溃了,所以你没有在你的问题中包含正确的代码。启用异常断点以查找您正在崩溃的行。

于 2013-03-08T18:11:01.987 回答