iOS 10. CoreData insert new object sig ABRT

守給你的承諾、 提交于 2019-12-06 10:16:48

First, no one has updated the documentation for the changes in Core Data for iOS 10 yet. Expect them to start showing up in the next couple of months (Apple included).

Second, with the new changes to Core Data, you should no longer need to cast on creation. Ideally you can now just do:

let word = Game(context: managedObjectContext)

And it will do the correct thing.

BTW, You should not need to call the entity "MyApp.Game". It should be just "Game" and that is probably the core of your error.

Finally, I have made it to work.
This isn't the first issue I have faced while working with CoreData in iOS 10, on xCode Beta.
So here is sequence of steps for adding your entities in xCode 8:
1.When creating your app pick CoreData option, this will create youAppName.xcdatamodeld file
2. choice youAppName.xcdatamodeld, add Entity button
3. add required attributes
4. use data Model inspector to setup code generation:
Entity Name "Game", Class Name "GameEntity", Module "Current Product Module", codegen "class definition"
5. cmd + s
6. Editor/create NSManagedObjectSubclass
7. You will get 3 new files:
GameEntity+CoreDataClass
GameEntity+CoreProperties
COREDATA
8. Comment out content of the file "COREDATA..."
9. youAppName.xcdatamodeld, data Model inspector, now when you have generated files, change Codeine property from "class definition" to "Manual/None"

Here is a code sample on how to add new objects:

let appDelegate = UIApplication.shared().delegate as! AppDelegate
                        let container = appDelegate.persistentContainer

                        let managedObjectContext = container.viewContext
                        for item in items {
                            let game = NSEntityDescription.insertNewObject(forEntityName: "Game", into: managedObjectContext) as! GameEntity
                            game.status = item.status
                            game.rs = item.rs
                            game.score = item.score


                            do {
                                try managedObjectContext.save()

here is a code sample on how to remove data:

let appDelegate = UIApplication.shared().delegate as! AppDelegate
        let container = appDelegate.persistentContainer
        let managedObjectContext = container.viewContext
        let fetchRequest: NSFetchRequest<GameEntity> = GameEntity.fetchRequest()
        do {
            let games = try managedObjectContext.fetch(fetchRequest) 
            for game in games {
                managedObjectContext.delete(game)
            }

First of all, Xcode 8 automatically generates the classes needed to represent the entities, so you don't have to create them manually. Instead of:

    let appDelegate = UIApplication.shared().delegate as! AppDelegate
    let container = appDelegate.persistentContainer
    let managedObjectContext = container.viewContext
    for item in items {
         let word = NSEntityDescription.insertNewObject(forEntityName: "MyApp.Game", into: managedObjectContext) as! Game

You could try this:

    let appDelegate = UIApplication.shared().delegate as! AppDelegate
    let container = appDelegate.persistentContainer
    let managedObjectContext = container.viewContext
    for item in items {
        let word = NSEntityDescription.insertNewObject(forEntityName: "Game", into: managedObjectContext) as! Game

But make sure to give the proper name to the class in Data Model Inspector Setting class name in Data Model Inspector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!