0

I'm attempting to save the username to core data, after getting the username as a string from a textfield, using the code below, but end up getting an error (also displayed below). I'm relatively new to swift and this code was written before I was familiar with programming conventions, so I apologize for the lack of organization and whatnot.

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

Error:

Using system font. 2017-06-08 09:25:03.111166 FInal Project[7950:225782] [error] error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'NSManagedObject' CoreData: error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'NSManagedObject'

2017-06-08 09:25:14.955615 FInal Project[7950:225782] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/student/Library/Developer/CoreSimulator/Devices/265EA47F-07A6-47C7-A6B4-5E62D37E72BA/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2017-06-08 09:25:14.979715 FInal Project[7950:225782] [MC] Reading from private effective user settings. 12345abcde username = Chris Out 2017-06-08 09:25:20.255362 FInal Project[7950:225782] [error] error: -addPersistentStoreWithType:SQLite configuration:(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; } ... returned error Error Domain=NSCocoaErrorDomain Code=134140 "(null)" UserInfo={sourceModel=() isEditable 1, entities

R. R
  • 55
  • 2
  • 8
  • Are you doing an automatic database version migration? If so you should see this answer: https://stackoverflow.com/questions/10833931/core-data-migration-of-attribute-from-string-to-integer-16 – Jon Rose Jun 08 '17 at 15:06
  • I'm not entirely sure as to what that is (Very new to swift and core data). I've tried following that one and it didn't seem to solve anything – R. R Jun 08 '17 at 17:52
  • Looks like you have a couple of problems - one of which is a duplicate of [this](https://stackoverflow.com/a/33307824/3985749). – pbasdf Jun 08 '17 at 23:00

0 Answers0