Swift信号SIGABRT错误

时间:2016-12-05 22:04:51

标签: swift core-data sigabrt

以下是代码:

 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错误。

3 个答案:

答案 0 :(得分:2)

看起来用于访问托管对象的界面已从swift2更改为swift3。正如本question/answer中所述,在您的情况下,您需要:

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

答案 1 :(得分:1)

基于我的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...]

答案 2 :(得分:1)

在Swift 3中应该是这样的:

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

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

希望是有帮助的