-2

Here's the code:

 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)

        }

    }

My issue is with the let mark = NSEntityDescription.insertNewObject(forEntityName: "Friend", into: context) as! Friend line, it was written in swift 2 and I changed all the code to swift 3 but this particular line gives me a signal SIGABRT error.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

2

Looks like the interface for accessing managed objects changed from swift2 to swift3. As explained in this question/answer, looks like in your case you need:

 let mark = Friend(context: context)
 mark.name = "Vuyo Nkabinde"
 mark.profileImageName = "zuckprofile"
Community
  • 1
  • 1
BHendricks
  • 4,423
  • 6
  • 32
  • 59
1

Based on my Swift 3.0 app:

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

[etc...]
Ron Diel
  • 1,354
  • 7
  • 10
1

It should be like this in Swift 3:

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

Please check syntax for Swift 3, It is like above.

Hope is helps

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Jitendra Modi
  • 2,344
  • 12
  • 34