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)")
}
}
}