首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表视图控制器cs193p iTunesU 9课

表视图控制器cs193p iTunesU 9课
EN

Stack Overflow用户
提问于 2013-03-08 15:39:55
回答 1查看 125关注 0票数 0

嗨,我正在学习iTunesU CS193P关于表视图和编译器的第9节课,请报告此错误,NSInternalInconsistencyException',原因:‘-_NSCFArray removeObjectAtIndex::mutating方法发送给不可变对象’。

我的模拟器是iPad 6.1

所以我有

一个名为GraphViewController.m的类

代码语言:javascript
复制
#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是设置器,我可以将数据发送到名为CalculatorTVC.m的tableview控制器上)

一个叫做CalculatorTVC.m的类

代码语言:javascript
复制
- (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中)

在我的故事板中,我有一个拆分视图...in MasterViewController,我有一个工具栏,工具栏上有一个带有表视图控制器(CalculatorTVC.m)连接的工具栏(CalculatorTVC.m),样式标识符是GraphTableView,还有一个圆圆的rect按钮,即这里描述的addFavorite。

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)

请帮帮我

谢谢你耐心的问候

好的,我找到它坠毁的地方..。

代码语言:javascript
复制
- (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 :program];

如果我把NSLOG放在这一行前面,我在输出NSLOG中看到了result...but,如果我把NSLOG放在后面,那么在输出中看不到任何东西

EN

回答 1

Stack Overflow用户

发布于 2013-03-08 18:11:01

代码语言:javascript
复制
 NSArray *program = [[NSUserDefaults standardUserDefaults]objectForKey:FAVORITE_KEY];
[segue.destinationViewController setPrograms:program];

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15297716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档