ios - Objective-C:为什么我在尝试从 nsmutablearray 中删除对象时遇到异常?

标签 ios objective-c nsmutablearray

我认为我正在尝试做的是非常基本的东西,我有 2 个包含 MyItem 对象的 NSMutableArrays。我想从一个数组中删除该项目并将其添加到另一个数组中,但是在尝试删除它时会引发异常,并且是的,为了节省您的提示,我对基本 OOP 的理解是失败的。我什至根本不处理枚举数组。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyItem *item = [self.completedItems objectAtIndex:indexPath.row]; 

    if(![self.toDoItems containsObject: item])
        [self.toDoItems addObject: item];

    if([self.completedItems containsObject: item])
        [self.completedItems removeObject:item];

    [self.tableView reloadData]; //reload contents on table view controller
}

这是异常(exception)

[__NSArrayI removeObject:]: 无法识别的选择器发送到实例 0x8d8bba0

这是数组的加载方式。它基本上是字典数组。
@interface ToDoListTableViewController : UITableViewController
    @property (strong) NSMutableArray *completedItems;
    @property (strong) NSMutableArray *toDoItems;  
@end

self.toDoItems = [[NSMutableArray alloc] init]; //initialize array
self.completedItems = [[NSMutableArray alloc] init]; //initialize array

 MyItem *newItem = [[MyItem alloc] init];
            newItem.text = [item valueForKey:@"text"];
            newItem.completed = [[item valueForKey:@"completed"] boolValue];
            newItem.date = [item valueForKey:@"date"];

            if (newItem.completed) {
                [self.completedItems addObject: newItem];
            }
            else
            {
                [self.toDoItems addObject: newItem];
            }

编辑
也许这段代码以某种方式使目标数组成为不可变数组?
UINavigationController *navigationController = segue.destinationViewController;
CompleteTableViewController *objChild = (CompleteTableViewController *)navigationController.topViewController;

if(objChild != nil){
     objChild.completedItems =  [self.completedItems copy];
     objChild.toDoItems =  [self.toDoItems copy];
}

提前感谢您指出我的问题。

最佳答案

正如异常(exception)所指出的 self.completedItemsNSArray ,而不是 NSMutableArray . NSArray没有这种方法removeObject .

更新!有问题代码!

if(objChild != nil){
     objChild.completedItems =  [self.completedItems copy]; //should be mutableCopy
     objChild.toDoItems =  [self.toDoItems copy]; //should be mutableCopy
}

关于ios - Objective-C:为什么我在尝试从 nsmutablearray 中删除对象时遇到异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658922/

相关文章:

ios - 如何在详细 View UIimage 中为右/左创建滑动手势

iphone - 如何在与操作相同的线程中取消 NSOperation?

iphone - 如何在 iphone 中将 NSMutableArray 保存到 plist 中

ios - 如何在 iPhone 中以编程方式创建多页 pdf

ios - Swift MGSwipeTableCell 滑动删除单元格

ios - Tableview单元格多个复选标记选择将单元格数据添加到单个数组Swift 4.2?

iphone - 使用 iPhone 上没有的字体

ios - 快速监视可变数组的计数。

ios - 无法从 Parse.com iOS 中的用户类返回对象

ios - 使用手机滚动日期选择器 : selectPickerWheelValue in appium