0
votes

I'm new to editing core data and struggling to get it to work for me, I have followed the example swift offer in their documentation but without success, Below is my code that when the saveButton is clicked the new data should get saved back into core data but I get a crash with this error message:

'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 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
please check your outlet connection there might be 2 connection for one label . Previously you might have given this name and the reference is still there.Tushar Sharma
It's definitely my code is incorrect :(Elfuthark

1 Answers

1
votes

Your saveButton function has this declaration:

let moc = self.managedObjectContext

Then it does things like this:

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

Except that NSManagedObjectContext does not have a property named oneWeekWeight. You'll have the same problem with all of the other lines attempting to set values on the context, because none of them will exist.

If you have an entity which has those properties, you need to have an instance of NSManagedObject which was created using that entity. You could either fetch an existing instance or create a new one. It's not clear from your code which one you want, but you probably know whether you're trying to create new data or edit existing data.