2016-12-05 586 views
-2

下面的代碼:斯威夫特信號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線,這是寫在迅速2,我改變了所有的代碼,以迅速3,但這一行給了我一個信號SIGABRT錯誤。

+0

請檢查我的答案 –

回答

2

看起來像訪問託管對象的界面從swift2更改爲swift3。在本question/answer解釋,看起來像你的情況,你需要:

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

根據我的雨燕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...] 
1

它應該是這樣的斯威夫特卡倫特3:

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

請檢查Swift 3的語法,如上所述。

希望是幫助