表视图控制器cs193p iTunesU 9课程



嗨,我正在学习iTunesU CS193P关于表视图的第九节课,编译器向我报告此错误NSInternalConsistencyException",原因:"-[__NSCFArray removeObjectAtIndex:]:正在更改发送到不可变对象的方法">

我的模拟器是iPad6.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是一个setter,我有数据要在我的表视图控制器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;
}

(程序是一个公共属性,我把GraphViewController.m中的数据放在这里(

在我的故事板中,我有一个拆分的视图。。。在MasterViewController中,我有一个带有条形按钮项目的工具栏,该项目与popover中的表视图控制器(CalculatorTVC.m(相连。样式标识符是GraphTableView,我有个圆形矩形按钮,它是addFavorite,在这里向上描述

错误是*由于未捕获的异常"NSInternalConferenceException"而终止应用程序,原因:"-[__NSCFArray removeObjectAtIndex:]:正在更改发送到不可变对象的方法"*第一次抛出调用堆栈:(0x1ca1012 0x10dee7e 0x1ca0deb 0x1d21c4f 0x1d21911 0x4a5b 0x8f65 0xdd8fb 0xdd9cf 0xc61bb 0xd6b4b 0x732dd 0x10f26bb0 0x229dfc0 0x229233c 0x22a0238 0x6b6e3 0x4f1476 0x870989a 0x4f255 0x489ef9 0x46ab99 0x46ac14 0x10f2705 0x262c0 0x262a64 0x10f2704 0x262c00 0x26258 0xe7021 0xe757f 0xe66e8 0x55cef 0x55f02 0x3d4a 0x25698 0x198 bfcdf9 0x1bfcad0 0x1c16bf5 0x1c16962 0x1c47bb6 0x1c46f44 0x1c46e1b 0x1bfb7e3 0x1bfb668 0x22ffc 0x24bd 0x23e5(libc++abi.dylib:调用terminate引发异常(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放在后面,我在输出中看不到任何东西

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

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

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

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

最新更新