-1

I'm following this introduction tutorial to this tutorial, and am completely new to Core Data.

Here is the class that produces the error (however the error sends me to the first line of AppDelegate with a sigabrt. The line that begins "let newUser" seems to produce the error, because the error dissappears when I comment it out.

@IBAction func btnSave(){
    let appDel:AppDelegate = (UIApplication.shared().delegate as! AppDelegate)
    let context:NSManagedObjectContext = appDel.persistentContainer.viewContext
    let newUser = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context) as NSManagedObject

    do {
        try context.save()
    } catch {}

    print("Object Saved.")    
}

This problem looks similar, but the answer is confusing to me and seems a bit overcomplicated

Since no one has posted about this error on the above Youtube video's comment section yet, I assume this is an error due to a change in Swift 3 (there was another error due to a change in Swift 2 which is documented in the comments).

Any help is greatly appreciated!

frame #0: 0x00000001067cbf06 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x00000001068ec4ec libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x000000010651e0b3 libsystem_c.dylib`abort + 129
frame #3: 0x00000001067ea43a libc++abi.dylib`abort_message + 266
frame #4: 0x000000010680ea9f libc++abi.dylib`default_terminate_handler() + 267
frame #5: 0x00000001055be59f libobjc.A.dylib`_objc_terminate() + 103
frame #6: 0x000000010680bc09 libc++abi.dylib`std::__terminate(void (*)()) + 8
frame #7: 0x000000010680b894 libc++abi.dylib`__cxa_rethrow + 99
frame #8: 0x00000001055be4b7 libobjc.A.dylib`objc_exception_rethrow + 40
frame #9: 0x00000001030f7bf1 CoreFoundation`CFRunLoopRunSpecific + 433
frame #10: 0x000000010850ea48 GraphicsServices`GSEventRunModal + 161
frame #11: 0x0000000103b30e8b UIKit`UIApplicationMain + 159
* frame #12: 0x0000000102b5dc6f CoreDataYoutube`main + 111 at AppDelegate.swift:5
frame #13: 0x00000001064726bd libdyld.dylib`start + 1
Community
  • 1
  • 1
PlateReverb
  • 654
  • 1
  • 7
  • 21
  • You're showing the non-useful part of the error readout. This error comes from an ObjC exception being thrown, so there should be console output above the stack trace you posted that describes the error. – rickster Sep 23 '16 at 18:34
  • In Swift 3 it's `UIApplication.shared.delegate` (without parentheses) – vadian Sep 23 '16 at 19:20
  • @vadian I just updated to Xcode 8.1 and I was required to do the change you mentioned.. however, the updated made my previous solution (listed as an "answer") now no longer works, and I don't know why... – PlateReverb Sep 23 '16 at 23:34

1 Answers1

2

I'm not 100% sure what solved this problem for me... but I think it may have been as simple as my .xcdatamodeld file was not saving the entity and attribute data I entered, and unlike everything else I've encountered in Xcode thusfar, it was required for me to explicitly save (CMD+S) before leaving the .xcdatamodeld file or else the data was immediately erased if I clicked on ViewController or anywhere else from Project navigator.

I'm not sure if this is an error only in the beta version of Xcode 8.0 I'm using, but it seems like it's time to update to 8.1...


After updating to 8.1, the above solution no longer works...


Final update... I'm not sure why the problem reappeared, but I solved it the same way, by deleting the .xcdatamodeld and making a new one. I also ran into trouble, because I changed the name of it from Model.xcdatamodeld to CoreDataYoutube.xcdatamodeld. If you do this, just remember you have to update the line in AppDelegate: let container = NSPersistentContainer(name: "Model") to match.

PlateReverb
  • 654
  • 1
  • 7
  • 21