此类与键Project1的键值编码不兼容。

时间:2019-05-30 10:52:34

标签: swift core-data

我是iOS开发的新手,有什么问题可以帮助我吗?谢谢你的进步。

  

未捕获的异常“ NSUnknownKeyException”,原因:   '[setValue:forUndefinedKey:]:   此类与键Project1的键值编码不兼容。'

    DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
    model.addRow(new Object[]
            {((JTextField) invo_date.getDateEditor().getUiComponent()).getText(),
                    ((JTextField) invo_date1.getDateEditor().getUiComponent()).getText(),
                    dec.getText(), BL.getText()});

    conn = JavaDb.ConnectDB();
    int p = 0;
    if (p == 0) {
        try {
            int rows = jTable2.getRowCount();
            String queryco = "update into status(date, to_date, description, containers) values( ?,?,?,?)"; 

            pst = conn.prepareStatement(queryco);
            for (int row = 0; row < rows; row++) {

                String datee = (String) jTable2.getValueAt(row, 0);
                String mpakadates = (String) jTable2.getValueAt(row, 1);
                String descri = (String) jTable2.getValueAt(row, 2);
                String total = (String) jTable2.getValueAt(row, 3);
                String container = (String) jTable2.getValueAt(row, 4);

                pst.setString(1, datee);
                pst.setString(2, mpakadates);
                pst.setString(3, descri);
                pst.setString(4, total);
                pst.setString(5, container);
                pst.addBatch();
            }
            pst.executeBatch();
            pst.execute();
            JOptionPane.showMessageDialog(this, "Data Saved Successfully");
            st.close();

        } catch (Exception e) {
            System.err.println("Got an exception! " + e);
            System.err.println(e.getMessage());
        }
    }

2 个答案:

答案 0 :(得分:2)

您应该在NSManagedObject对象中设置值,而不是在NSEntityDescription对象中设置

let entity = NSEntityDescription.entity(forEntityName: "Projects", in: context)
for n in 0..<arrProj.count {
    let projects = NSManagedObject(entity: entity!, insertInto: context)
    projects?.setValue(item.Project, forKey: "project1")
    projects?.setValue(item.Project2, forKey: "project2")
    //...
}

答案 1 :(得分:2)

请使用insertobject查询对此进行检查,您可以将数据保存在核心数据中。

    let  context  = appdelegate.persistentContainer.viewContext
    let projects = NSEntityDescription.insertNewObject(forEntityName: "Projects", into: context) as! Projects

    projects.project1 = item.Project
    projects.project2 = item.Project2
    or
    projects?.setValue(item.Project, forKey: "project1")
    projects?.setValue(item.Project2, forKey: "project2")
    projects?.setValue(item.ID, forKey: "projectid")
    projects?.setValue(item.radius, forKey: "radius")

    do{
    try context.save()
    }catch{
    print("error")
    }