Swift 信号 SIGABRT 错误

Posted

技术标签:

【中文标题】Swift 信号 SIGABRT 错误【英文标题】:Swift signal SIGABRT error 【发布时间】:2016-12-05 22:04:50 【问题描述】:

代码如下:

 func setupData() 

    clearData()

    let delegate = UIApplication.shared.delegate as? AppDelegate

    if let context = delegate?.persistentContainer.viewContext 



        let mark = NSEntityDescription.insertNewObject(forEntityName: "Friend", into: context) as! Friend
        mark.name = "Vuyo Nkabinde"
        mark.profileImageName = "zuckprofile"


        let message = NSEntityDescription.insertNewObject(forEntityName: "Message", into: context) as! Message
        message.friend = mark
        message.text = "Hello, my name is Mark. Nice to meet you..."
        message.date = NSDate()

        let steve = NSEntityDescription.insertNewObject(forEntityName: "Friend", into: context) as! Friend
        steve.name = "Steve Jobs"
        steve.profileImageName = "steve_profile"

        let messagesSteve = NSEntityDescription.insertNewObject(forEntityName: "Message", into: context) as! Message
        messagesSteve.friend = steve
        messagesSteve.text = "Code is the most innovative company I have ever seen..."
        messagesSteve.date = NSDate()

        do 
        try(context.save())
         catch let err 
            print(err)

        

    

我的问题是 let mark = NSEntityDescription.insertNewObject(forEntityName: "Friend", into: context) as! Friend 行,它是用 swift 2 编写的,我将所有代码都更改为 swift 3,但这一行给了我一个信号 SIGABRT 错误。

【问题讨论】:

请检查我的答案 【参考方案1】:

看起来访问托管对象的接口从 swift2 更改为 swift3。正如question/answer 中所解释的那样,在您的情况下,您需要:

 let mark = Friend(context: context)
 mark.name = "Vuyo Nkabinde"
 mark.profileImageName = "zuckprofile"

【讨论】:

【参考方案2】:

基于我的 Swift 3.0 应用:

let entity = NSEntityDescription.entity(forEntityName: "Friend", in: context)
let mark = Friend(entity: entity!, insertInto: context)
mark.name = "Vuyo Nkabinde"
mark.profileImageName = "zuckprofile"

[etc...]

【讨论】:

【参考方案3】:

在 Swift 3 中应该是这样的:

let employee = NSEntityDescription.insertNewObjectForEntityForName("Friend", inManagedObjectContext: context) as! Friend

请检查Swift 3的语法,如上。

希望有帮助

【讨论】:

以上是关于Swift 信号 SIGABRT 错误的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift 错误:执行被中断,原因:信号 SIGABRT

Swift:线程 1:信号 SIGABRT

Swift”线程 1:PresentViewController 中的信号 SIGABRT

在 Swift Playground 中创建 UIManagedDocument - 信号 SIGABRT 错误

无法保存数据 --- Swift、IOS、Core 数据 --- 线程 1:信号 SIGABRT

Thread1:Swift 3 中的信号 SIGABRT