使用NSMutableArray删除UITableViewCell行会引发异常

时间:2016-06-13 01:04:46

标签: swift uitableview nsmutablearray nsuserdefaults

我有一个tableview,每个单元格内的数据都是NSMutableArray。我想在每次向左滑动时删除一行tableview并按下删除按钮。但是使用NSMutableArray很困难。每当我输入代码toDoItems.removeObjectAtIndex(indexPath.row)时,我都会得到一个Sigabrt。

我得到的错误是

  

使用NSException类型的未捕获异常终止' NSInternalInconsistencyException',原因:' - [__ NSCFArray removeObjectAtIndex:]:发送到不可变对象的变异方法'

有关此问题的任何帮助吗?

以下是与问题相关的代码。

包含toDoItems的ViewDidLoad代码:

let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()

        let itemListFromUserDefaults:NSMutableArray? = userDefaults.objectForKey("itemList") as? NSMutableArray

        if ((itemListFromUserDefaults) != nil){
            toDoItems = itemListFromUserDefaults!
        }

我的NSMutableArray变量

var toDoItems:NSMutableArray! = NSMutableArray()

我的commitEditingStyle函数:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if(editingStyle == UITableViewCellEditingStyle.Delete){


        //This code gives me the error and Sigabrt
        toDoItems.removeObjectAtIndex(indexPath.row)



        tbl?.reloadData();


    }

我的numberOfRowsInSection函数:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{


        return toDoItems.count
    }

我的cellForRowAtIndexPath函数:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell{

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableView

            let toDoItem:NSDictionary = toDoItems.objectAtIndex(indexPath.row) as! NSDictionary
            cell.lbl.text = toDoItem.objectForKey("itemTitel") as? String

            return cell



}

这是错误

MyPlanner [12373:461118] *由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' - [__ NSCFArray removeObjectAtIndex:]:发送到不可变对象的变异方法' * 第一次抛出调用堆栈: (     0 CoreFoundation 0x0000000109df3d85 exceptionPreprocess + 165     1 libobjc.A.dylib 0x0000000109867deb objc_exception_throw + 48     2 CoreFoundation 0x0000000109df3cbd + [NSException raise:format:] + 205     3 CoreFoundation 0x0000000109de9dae - [__ NSCFArray removeObjectAtIndex:] + 94     4 MyPlanner 0x0000000109210c53 _TTSf4dg_n_g_n___TFC9MyPlanner14ViewController9tableViewfTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 131     5 MyPlanner 0x000000010920fc3d _TToFC9MyPlanner14ViewController9tableViewfTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 61     6 UIKit 0x000000010ac124c0 - [UITableView animateDeletionOfRowWithCell:] + 205     7 UIKit 0x000000010abe7a3e __52- [UITableView _swipeActionButtonsForRowAtIndexPath:] _ block_invoke + 80     8 UIKit 0x000000010ac13eaa - [UITableView _actionButton:pushInCell:] + 172     9 UIKit 0x000000010ae3aae6 - [UITableViewCell _actionButtonPushed:] + 82     10 UIKit 0x000000010aab4a8d - [UIApplication sendAction:to:from:forEvent:] + 92     11 UIKit 0x000000010ac27e67 - [UIControl sendAction:to:forEvent:] + 67     12 UIKit 0x000000010ac28143 - [UIControl _sendActionsForEvents:withEvent:] + 327     13 UIKit 0x000000010ac27263 - [UIControl touchesEnded:withEvent:] + 601     14 UIKit 0x000000010af9cc52 _UIGestureRecognizerUpdate + 10279     15 UIKit 0x000000010ab2748e - [UIWindow _sendGesturesForEvent:] + 1137     16 UIKit 0x000000010ab286c4 - [UIWindow sendEvent:] + 849     17 UIKit 0x000000010aad3dc6 - [UIApplication sendEvent:] + 263     18 UIKit 0x000000010aaad553 _UIApplicationHandleEventQueue + 6660     19 CoreFoundation 0x0000000109d19301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17     20 CoreFoundation 0x0000000109d0f22c __CFRunLoopDoSources0 + 556     21 CoreFoundation 0x0000000109d0e6e3 __CFRunLoopRun + 867     22 CoreFoundation 0x0000000109d0e0f8 CFRunLoopRunSpecific + 488     23 GraphicsServices 0x000000010ec78ad2 GSEventRunModal + 161     24 UIKit 0x000000010aab2f09 UIApplicationMain + 171     25 MyPlanner 0x00000001092134fd main + 125     26 libdyld.dylib 0x000000010d43d92d start + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (LLDB)

以下是我在toDoItems中添加信息(添加项目)的代码。

@IBAction func ClickedforSelection(sender: AnyObject) {

    if delegate != nil {
        let information : NSString = txt1.text!

        delegate!.userDidEntredInformation(information)

    }

    let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()

    var itemList:NSMutableArray? = userDefaults.objectForKey("itemList") as? NSMutableArray

    let dataSet:NSMutableDictionary = NSMutableDictionary()
    dataSet.setObject(txt.text!, forKey: "itemTitel")


    if ((itemList) != nil){ // data already available
        let newMutableList:NSMutableArray = NSMutableArray();

        for dict:AnyObject in itemList!{
            newMutableList.addObject(dict as! NSDictionary)
        }

        userDefaults.removeObjectForKey("itemList")
        newMutableList.addObject(dataSet)
        userDefaults.setObject(newMutableList, forKey: "itemList")


    }else{ // This is the first todo item in the list
        userDefaults.removeObjectForKey("itemList")
        itemList = NSMutableArray()
        itemList!.addObject(dataSet)
        userDefaults.setObject(itemList, forKey: "itemList")
    }

    userDefaults.synchronize()

1 个答案:

答案 0 :(得分:2)

NSUserDefaults objectForKey将始终返回不可变对象。简单地向下投射它不会使它变得可变。您需要从返回的不可变数组创建一个可变数组:

let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()

if let itemListFromUserDefaults = userDefaults.objectForKey("itemList") as? NSArray
    toDoItems = itemListFromUserDefaults.mutableCopy()
}