本文介绍了iOS 10. CoreData插入新对象sig ABRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过forEntityName:Game,MyApp.Game。



在我的dataManager文件中:

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

让managedObjectContext = container.viewContext
项目中的项目{
let word = NSEntityDescription.insertNewObject(forEntityName:MyApp。游戏,into:managedObjectContext)as! Game + coreDataProperties文件中的游戏
  extension游戏{

@nonobjc class func fetchRequest() - > NSFetchRequest< Game> {
return NSFetchRequest< Game>(entityName:Game);
}

在Game + coreDataClass

  class Game:NSManagedObject {

}

我使用.xcdatamodeld生成CoreData文件:在对象检查器中我指定名称:游戏,类游戏,模块当前产品模块,codegen类定义,然后我使用编辑器/ ,我得到3个文件后,我将codegen属性设置为手动/无



我试图转换到游戏中运行时错误:signal SIGABRT

  NSEntityDescription.insertNewObject(forEntityName:MyApp.Game,into:managedObjectContext)as!游戏

我做错了什么?

解决方案

最后,我已经做了它工作。

这不是我在iOS 10中使用CoreData在xCode Beta上遇到的第一个问题。

这里是添加实体的步骤序列在xCode 8中:

1.创建应用程序时,选择CoreData选项,将创建youAppName.xcdatamodeld文件

2.选择youAppName.xcdatamodeld,添加实体按钮

3.添加必需的属性

4.使用数据模型检查器设置代码生成:

实体名称Game,类名称GameEntity,模块当前产品模块 ,codegen类定义

5. cmd + s

6.编辑器/创建NSManagedObjectSubclass

7.您将获得3个新文件:

GameEntity + CoreDataClass

GameEntity + CoreProperties

COREDATA

8.注释文件的内容 COREDATA ...

9. youAppName.xcdatamodeld,数据模型检查器,现在当您生成文件时,将Codeine属性从类定义更改为手动/无 p>

下面是关于如何添加新对象的代码示例:

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

让managedObjectContext = container.viewContext
项目中的项目{
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()

这里是关于如何删除数据的代码示例: p>

  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)
游戏中的游戏{
managedObjectContext.delete(game)
}


I tried forEntityName: "Game", "MyApp.Game".

in my dataManagerFile:

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

in Game+coreDataProperties file

extension Game {

    @nonobjc class func fetchRequest() -> NSFetchRequest<Game> {
        return NSFetchRequest<Game>(entityName: "Game");
    }

in Game+coreDataClass

class Game: NSManagedObject {

}

I generated files for CoreData using .xcdatamodeld: in object inspector I specified Name: "Game", Class "Game", Module "Current Product Module", codegen "class definition", then I used Editor/create subclass, after I got 3 files I set codegen property to "Manual/None"

I got run-time error: "signal SIGABRT" when trying to cast to Game in:

NSEntityDescription.insertNewObject(forEntityName: "MyApp.Game", into: managedObjectContext) as! Game

What am I doing wrong?Also could you advice me a good manual on CoreData, with latest changes applied?

解决方案

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)
            }

这篇关于iOS 10. CoreData插入新对象sig ABRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:03