线程1:删除核心数据记录时的EXC断点

时间:2018-02-15 16:38:18

标签: ios swift core-data exc-bad-access

我有一组存储在核心数据中的项目,称为费用,这些项目被分组为费用。现在当我删除一个集合时,我希望该集合中的所有费用也删除并暗示以下代码:

 if editingStyle == .delete{
        let refreshAlert = UIAlertController(title: "Delete Collection", message: "All expenses in this collection will also be lost.", preferredStyle: UIAlertControllerStyle.alert)

        refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
            self.collections = CoreDataHelper.retrieveCollections()
        }))

        refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in

            let collection = self.collections[indexPath.row]
            print(self.collections[indexPath.row].title!)
            for expense in self.collectionExpensesDelete{
                if expense.collection == self.collections[indexPath.row].title{
                    if let expenseDeleter = self.collectionExpensesDelete.index(of: expense) {
                        self.collectionExpensesDelete.remove(at: expenseDeleter)
                        CoreDataHelper.deleteExpense(expense: expense)
                    }
                }
            }
            CoreDataHelper.deleteCollection(collection: collection)
            self.collections.remove(at: indexPath.row)
            self.collections = CoreDataHelper.retrieveCollections()
            CoreDataHelper.save()
        }))

并且此代码有效,但每当我进入我的主页时,我都会收到线程1的错误:BAD异常并且它会崩溃应用程序。当我重新打开应用程序时,费用将被删除,但只有在应用程序崩溃后才能使用。这是我使用的代码以及我得到错误的地方;

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "mainCell", for: indexPath) as! MainTableViewCell

    cell.layoutMargins = UIEdgeInsets.zero

    let row = indexPath.row

    let expense = expenseFiltered[row]

    CoreDataHelper.save()

    var expenseAmountDisplayed:String = ExpensesAdditions().convertToMoney(expense.amount)

    cell.expenseLabel.text = expense.name
    cell.expenseLabel.adjustsFontSizeToFitWidth = true
    cell.expenseAmount.adjustsFontSizeToFitWidth = true

    cell.expenseCategory.text = expense.category
    cell.expenseCollection.text = expense.collection

    var finalDisplayed:String = expense.currencySymbol! + " " + expenseAmountDisplayed
    cell.expenseAmount.text = finalDisplayed

    if expense.expense {
        cell.expenseAmount.textColor = UIColor.red
    }else if expense.income {
        cell.expenseAmount.textColor = UIColor(red:0.49, green:0.83, blue:0.13, alpha:1.0)
    }

    if !expense.credit && expense.expense{
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Expense Icon")
    }
    else if !expense.credit && expense.income{
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Income Icon")
    }
    else if expense.credit && expense.income{
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Income Icon")
    }
    else if expense.credit && expense.expense{
        cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Expense Icon")
    }

    return cell

}

我认为阅读费用是一个问题,但不明白为什么,因为一旦费用从内存中删除,就不应该再存在了。错误就出现了:

var finalDisplayed:String = expense.currencySymbol! + " " + expenseAmountDisplayed

0 个答案:

没有答案