所有插座都是正确的,此类不适用于键列表的键值编码

时间:2019-05-26 07:26:07

标签: swift

我一直在获取此类不符合键listTitle错误的键值编码,但是我的所有出口都是正确的。

我试图重新连接所有插座,并确保没有问题,但是此错误仍然存​​在。

@IBOutlet weak var listTitle: UILabel!
@IBOutlet weak var itemTextField: UITextField!
@IBOutlet weak var likeRate: UILabel!
@IBOutlet weak var slider: UISlider!
@IBOutlet weak var counter: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    listTitle.text = addedListTitle

    slider.value = 1
    likeRate.text = "1"
}


@IBAction func addPressed(_ sender: Any) {

    if (itemTextField.text!==""){
        counter.text = "You must enter a name for the item"
    }
    else {

        elementCount += 1
        addedItem[itemTextField.text!] = elementRate

        if elementCount < 2
        {
           counter.text = "You have \(elementCount) element in the list"
        }
        else
        {
            counter.text = "You have \(elementCount) elements in the list"
        }
        itemTextField.text = ""
        slider.value = 1
        likeRate.text = "1"
        elementRate = 1
    }
}
@IBAction func finishPressed(_ sender: Any) {

    if (elementCount == 0){
        counter.text  = "You must enter at least one element"
    }
    else{
        addTitle()
        addItems()

        performSegue(withIdentifier: "addToGenerate", sender: self)
    }
}

@IBAction func cancelPressed(_ sender: Any) {

    performSegue(withIdentifier: "addToHome", sender: self)
}

func addTitle() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let newList = NSEntityDescription.insertNewObject(forEntityName: "Lists", into: context)

    newList.setValue(addedListTitle, forKey: "listTitle")

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

func addItems() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let newItemList = NSEntityDescription.insertNewObject(forEntityName: "Items", into: context)

    for item in addedItem {
        newItemList.setValue(item.key, forKey: "itemName")
        newItemList.setValue(item.value, forKey: "rate")
        newItemList.setValue(addedListTitle, forKey: "listTitle")
    }

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

    }
}

}

    2019-05-26 03:05:15.230138-0400 Selector[4391:283284] *** Terminating
    app due to uncaught exception 'NSUnknownKeyException', reason:
    '[<NSFetchRequest 0x600001c089a0> valueForUndefinedKey:]: this class
    is not key value coding-compliant for the key listTitle.'

1 个答案:

答案 0 :(得分:0)

您在loadData类的FirstTableViewController方法中遇到问题。 您正在尝试从request NSFetchRequest对象获取值。您应该从NSManagedObject对象result获取值。

更改

if let listTitle = request.value(forKey: "listTitle") as? String {

if let listTitle = result.value(forKey: "listTitle") as? String {