【问题标题】:Cannot find 'NSEntityDescription' and 'NSManagedObject' in scope [closed]在范围内找不到“NSEntityDescription”和“NSManagedObject”[关闭]
【发布时间】:2022-01-07 01:01:46
【问题描述】:

我正在尝试将 textfield.text 添加到我的 CoreData 中,我是 coredata 的新手,因此我在 Google 上搜索并复制了操作方法,但 xCode 返回错误:Cannot find 'NSEntityDescription' in scopeCannot find 'NSManagedObject' in scope 是否有任何简单的解决方案解决这个问题?

    @objc func textFieldDidChange(_ textField: UITextField) {
        print(textField.text ?? "username581293491822")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
        let newUser = NSManagedObject(entity: entity!, insertInto: context)
        newUser.setValue(textField.text ?? "username12312235234", forKey: "username")
    }

【问题讨论】:

  • @JoakimDanielson 实际上名称不正确,我理解您的困惑,`usernameTextfield.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingDidEnd)`我的viewDidLoad 中的代码,你可以在.editingDidEnd 上看到它的设置,所以我想不会有问题。

标签: ios swift core-data


【解决方案1】:

你需要添加

import CoreData

【讨论】:

    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2021-06-18
    • 2021-01-15
    相关资源
    最近更新 更多