CoreData:错误:无法在NSManagedObject类'NSManagedObject'上调用指定的初始值设定项

时间:2017-06-08 13:53:02

标签: ios swift entity-framework core-data

我正在尝试将用户名保存到核心数据,在使用下面的代码从文本字段获取用户名作为字符串后,但最终得到错误(也显示在下面)。我对swift相对较新,这段代码是在我熟悉编程约定之前编写的,所以我为缺乏组织和诸如此类的东西而道歉。

// Once button is pressed
@IBAction func NextButtonPressed(_ sender: Any) {
    // Gets user's name from textfield
    print("12345abcde")

    self.username = NameInput.text!

    // If textfield is not empty, moves to next storyboard

    if username.isEmpty == false && username != "" {

        print("username = \(username)")
        //function saves text field info to core data
        // Moves to next view controller
        //performSegue(withIdentifier: "fistBump", sender: nil)
        print("Out ")
        save(username: username)

    }
    else {
        print("text field is empty")
        NextButton.isEnabled = false
        return
    }

}

func save(username: String) {

    //making sure that you're using correct AppDelegate
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }

    //get context
    let managedContext = appDelegate.persistentContainer.viewContext

    //get correct entity
    let entity = NSEntityDescription.entity(forEntityName: "UserInfo",
                                            in: managedContext)!

    //new object
    user = NSManagedObject(entity: entity,
                           insertInto: managedContext)

    print("about to save new info for book (first view controller)")

    user.setValue(username, forKeyPath: "name")

    do {
        try managedContext.save()
        print("Saved")
    } catch _ as NSError {
        print("Could not save.")
    }
}

错误:

使用系统字体。 2017-06-08 09:25:03.111166 FInal Project [7950:225782] [错误]错误:CoreData:错误:无法在NSManagedObject类'NSManagedObject'上调用指定的初始值设定项 CoreData:错误:CoreData:错误:无法在NSManagedObject类'NSManagedObject'上调用指定的初始值设定项

2017-06-08 09:25:14.955615 FInal Project [7950:225782] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是/ Users / student / Library / Developer / CoreSimulator / Devices / 265EA47F -07A6-47C7-A6B4-5E62D37E72BA /数据/容器/共享/ SystemGroup / systemgroup.com.apple.configurationprofiles 2017-06-08 09:25:14.979715 FInal Project [7950:225782] [MC]从私人有效用户设置中读取。 12345abcde 用户名=克里斯 退房 2017-06-08 09:25:20.255362 FInal Project [7950:225782] [错误]错误:-addPersistentStoreWithType:SQLite配置:(null)URL:file:/// Users / student / Library / Developer / CoreSimulator / Devices / 265EA47F-07A6-47C7-A6B4-5E62D37E72BA / data / Containers / Data / Application / 497780B2-15DE-475A-AD66-242FB44D1C72 / Library / Application%20Support / FInal_Project.sqlite options:{     NSInferMappingModelAutomaticallyOption = 1;     NSMigratePersistentStoresAutomaticallyOption = 1; } ...返回错误错误Domain = NSCocoaErrorDomain Code = 134140“(null)”UserInfo = {sourceModel =()isEditable 1,entities

0 个答案:

没有答案