编辑核心数据会产生错误,此类不是密钥的编码兼容键

时间:2017-06-27 18:51:28

标签: ios swift core-data nsfetchrequest

我是新手编辑核心数据并努力让它为我工作,我在他们的文档中遵循了示例快速提供但没有成功,下面是我的代码,当点击saveButton时,新数据应该得到保存回核心数据,但我收到此错误消息:

'[setValue:forUndefinedKey:]:此类不是键值oneWeekWeight的键值编码。'

    import UIKit
    import CoreData

class WeekStatsViewController: UIViewController {

    var client: Client? = nil
    var managedObjectContext: NSManagedObjectContext!

    @IBOutlet weak var dobLabel: UILabel!
    @IBOutlet weak var telephoneLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!
    @IBOutlet weak var heightLabel: UILabel!

    @IBOutlet weak var weightTextField: UITextField!
    @IBOutlet weak var weekTextField: UITextField!

    @IBOutlet weak var neckTextField: UITextField!
    @IBOutlet weak var shouldersTextField: UITextField!
    @IBOutlet weak var chestTextField: UITextField!
    @IBOutlet weak var bicepTextField: UITextField!
    @IBOutlet weak var waistTextField: UITextField!
    @IBOutlet weak var hipsTextField: UITextField!
    @IBOutlet weak var thighTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

        // Do any additional setup after loading the view.
        navigationItem.title = client!.name
        dobLabel.text = client!.dob
        telephoneLabel.text = client!.telephone
        emailLabel.text = client!.email
        heightLabel.text = String(describing: client!.height)
    }

    // Dismiss keyboard when empty space tapped
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        weightTextField.endEditing(true)
        weekTextField.endEditing(true)
        neckTextField.endEditing(true)
        shouldersTextField.endEditing(true)
        chestTextField.endEditing(true)
        bicepTextField.endEditing(true)
        waistTextField.endEditing(true)
        hipsTextField.endEditing(true)
        thighTextField.endEditing(true)
    }

    @IBAction func saveButton(_ sender: Any) {

        let editWeight = Double(weightTextField.text!)
        let editWeek = weekTextField.text
        let editNeck = neckTextField.text
        let editShoulders = shouldersTextField.text
        let editChest = chestTextField.text
        let editBicep = bicepTextField.text
        let editWaist = waistTextField.text
        let editHips = hipsTextField.text
        let editThigh = thighTextField.text

        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Client")

        do {
            let fetchedClients = try managedObjectContext.execute(fetchRequest)

            fetchedClients.setValue(editWeight, forKey: "oneWeekWeight")
            fetchedClients.setValue(editWeek, forKey: "weekOneSelected")
            fetchedClients.setValue(editNeck, forKey: "oneWeekNeck")
            fetchedClients.setValue(editShoulders, forKey: "oneWeekShoulders")
            fetchedClients.setValue(editChest, forKey: "oneWeekChest")
            fetchedClients.setValue(editBicep, forKey: "oneWeekBicep")
            fetchedClients.setValue(editWaist, forKey: "oneWeekWaist")
            fetchedClients.setValue(editHips, forKey: "oneWeekHips")
            fetchedClients.setValue(editThigh, forKey: "oneWeekThigh")

            try managedObjectContext.save()


        } catch {
            fatalError("Failed to fetch clients: \(error)")
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您的saveButton函数有此声明:

let moc = self.managedObjectContext

然后它做了这样的事情:

moc?.setValue(editWeight, forKey: "oneWeekWeight")

NSManagedObjectContext没有名为oneWeekWeight的属性外。尝试在上下文中设置值的所有其他行都会遇到同样的问题,因为它们都不存在。

如果您有一个具有这些属性的实体,则需要使用该实体创建的NSManagedObject实例。您可以获取现有实例或创建新实例。从您的代码中不清楚您想要哪一个,但您可能知道您是在尝试创建新数据还是编辑现有数据。