Ios 编辑核心数据会产生错误此类不符合密钥的键值编码

Ios 编辑核心数据会产生错误此类不符合密钥的键值编码,ios,swift,core-data,nsfetchrequest,Ios,Swift,Core Data,Nsfetchrequest,我刚开始编辑核心数据,并努力让它为我工作,我在他们的文档中遵循了swift提供的示例,但没有成功,下面是我的代码,当单击Save按钮时,新数据应保存回核心数据,但我遇到了此错误消息的崩溃: “[setValue:forUndefinedKey::]:对于键oneWeekWeight,此类不符合键值编码。” import UIKit import CoreData class WeekStatsViewController: UIViewController { var

我刚开始编辑核心数据,并努力让它为我工作,我在他们的文档中遵循了swift提供的示例,但没有成功,下面是我的代码,当单击Save按钮时,新数据应保存回核心数据,但我遇到了此错误消息的崩溃:

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

    }

}
导入UIKit
导入CoreData
类WeekStatsViewController:UIViewController{
var客户端:客户端?=nil
var managedObjectContext:NSManagedObjectContext!
@IBVAR标签:UILabel!
@IBLABEL:UILabel!
@iblabel:UILabel!
@IBVAR弱var高度标签:UILabel!
@IBOutlet弱var权重文本字段:UITextField!
@IBEXTEXTFIELD:UITextField!
@ibextfield:UITextField!
@IBOutlet弱var shouldersTextField:UITextField!
@ibextfield:UITextField!
@IBOutlet弱变量bicepTextField:UITextField!
@ibextfield:UITextField!
@Ibstextfield:UITextField!
@IBOutlet弱var thighTextField:UITextField!
重写func viewDidLoad(){
super.viewDidLoad()
managedObjectContext=(UIApplication.shared.delegate为!AppDelegate)。persistentContainer.viewContext
//加载视图后执行任何其他设置。
navigationItem.title=客户端!.name
dobLabel.text=客户端!.dob
telephoneLabel.text=client!.telephone
emailLabel.text=客户端
heightLabel.text=字符串(描述:client!.height)
}
//点击空白时关闭键盘
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
weightTextField.endEditing(真)
weekTextField.endEditing(真)
neckTextField.endEditing(真)
shouldersTextField.endEditing(真)
chestTextField.endEditing(真)
bicepTextField.endEditing(true)
WeightTextField.endEditing(真)
hipsTextField.endEditing(真)
thighTextField.endEditing(真)
}
@iAction func saveButton(\发送方:任意){
让editWeight=Double(weightTextField.text!)
让editWeek=weekTextField.text
让editNeck=neckTextField.text
让EditBeals=shouldersTextField.text
让editChest=chestTextField.text
让editbiep=bicepTextField.text
让EditHeild=腰围文本字段.text
让editHips=hipsTextField.text
让editThigh=ThightXtfield.text
let fetchRequest=NSFetchRequest(entityName:“客户端”)
做{
让fetchedClients=try managedObjectContext.execute(fetchRequest)
gethedClients.setValue(editWeight,forKey:“oneWeekWeight”)
fetchedClients.setValue(editWeek,forKey:“weekOneSelected”)
fetchedClients.setValue(editNeck,forKey:“oneWeekNeck”)
gethedClients.setValue(EditBeals,forKey:“oneWeekShoulders”)
gethedClients.setValue(editChest,forKey:“oneWeekChest”)
gethedClients.setValue(editBicep,forKey:“oneWeekBicep”)
gethedClients.setValue(EditShield,forKey:“oneWeekWaist”)
fetchedClients.setValue(editHips,forKey:“oneWeekHips”)
fetchedClients.setValue(editThigh,forKey:“oneWeekThigh”)
请尝试managedObjectContext.save()
}抓住{
fatalError(“无法获取客户端:\(错误)”)
}
}
}

您的
保存按钮
函数具有以下声明:

let moc = self.managedObjectContext
然后它会这样做:

moc?.setValue(editWeight, forKey: "oneWeekWeight")
除了
NSManagedObjectContext
没有名为
oneWeekWeight
的属性之外。其他所有试图在上下文中设置值的行都会遇到同样的问题,因为它们都不存在


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

嗨,Tom,我在等待帮助时一直在尝试解决这一问题,发现您的观点完全相同,当我注释掉第一次出现错误的那一行时,所有的行都是一样的。我正在尝试编辑现有数据,并且认为我自己很困惑:)看起来您想要使用获取请求的结果之一。不过,您可能需要添加一个获取谓词,因为可能会有多个结果。请您检查一下我更新的代码,并告诉我哪里出了问题,我尝试的所有操作都会导致相同的原始错误。如果你不太忙,我真的很感激。感谢您的新代码尝试在
fetchedClients
上使用相同的键,这是一个数组。不知何故,您需要获得单个实例——例如,从该数组中选择一个条目。你如何选择一个取决于你的应用程序需要如何工作。在您获取的所有结果中,您希望更新哪一个?请检查您的插座连接一个标签可能有两个连接。以前您可能给出了此名称,但引用仍然存在。这肯定是因为我的代码不正确:(