首页 文章

添加5个以上相关对象时,核心数据应用程序崩溃

提问于
浏览
0

我有两个核心数据实体,每个实体与另一个具有多对多关系 . 我正在使用 tableView 来允许用户选择哪个salaryClasses与给定的Proposal相关 . 该项目是Swift 3,iOS 10.2.1,我正在使用Editor菜单中的 NSManagedObject 子类 .

这一切都很有效,直到我尝试将第六个salaryClass添加到Proposal . Note: If I try to add any value other than the first 5 into the WageClass array, it crashes. But when I print the array it prints as many values as there are in the array. 应用程序因错误而崩溃:在展开Optional值时意外发现nil .

我还得到一个Thread 1:EXC_BREAKPOINT错误,但Breakpoints导航器和lldb没有列出断点 .

我通过尝试单独添加wageClasses,通过添加不同的顺序,并通过创建最多10个wageClasses(以查看它是否是最后创建的salaryClass未在 tableView 中加载的问题)来测试这个,但没有运气,我得到了相同的结果 .

这是我要添加的地方:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "AddWageClassCell", for: indexPath)

    let wageClass = wageClasses[indexPath.row]


    let wageClassStatus = wageClass.value(forKey: "checked") as? Bool ?? true

    cell.textLabel?.text = wageClass.value(forKey: "wageClassName") as? String

    var accessoryType = UITableViewCellAccessoryType.none
    var tintColor = UIColor.clear

    if (wageClassStatus) {
        accessoryType = UITableViewCellAccessoryType.checkmark
        tintColor = UIColor.green

    }

    cell.accessoryType=accessoryType
    cell.tintColor = tintColor


    return cell

}

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {


        let wageClassObject = self.wageClasses[indexPath.row]

        var wageClassStatus = wageClassObject.value(forKey: "checked") as? Bool ?? false

        wageClassObject.setValue(!wageClassStatus, forKey:"checked")





        proposalToEdit?.addToWageClasses(wageClassObject)




        do {
            try 
            ad.saveContext()
        } catch let error as NSError {
            print("Cannot save object: \(error), \(error.localizedDescription)")
        }

        tableView.deselectRow(at: indexPath,animated:false)

        tableView.reloadRows(at: [indexPath], with: .none)






    }

感谢您的意见!

1 回答

  • 0

    我终于意识到当我在单独的xib上的单独tableView上使用 wageClasses 格式化单元格时,我强制解包变量 . 当我把它包装在if let语句中时

    if let cell = tableView.cellForRow(at: indexPath) as? WageCell {
                     configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
                } else {
                    print("something is nil")
                }
    
                }
    

    崩溃消失了 .

相关问题