0

I going through Cocoa Programming for OS X book by Big Nerd Ranch, and I'm stuck on Chapter 9 -- working with NSArrayController. This book is not up to date, so some things I had to search online to make it work.

I'm stuck on binding a TableViewCell to my object's key.

So I have an object:

import Foundation

   class Employee: NSObject {
       var name: String? = "New Employee" 
       var raise: Float = 0.05
   }

This is binded to the NSArrayController Content Array.

The Document controller where the Employee object is used is:

import Cocoa

class Document: NSDocument {

    @objc dynamic var employees: [Employee] = []

    override init() {
        super.init()
        // Add your subclass-specific initialization here.
    }

    override class var autosavesInPlace: Bool {
        return true
    }

    override var windowNibName: NSNib.Name? {
        return NSNib.Name("Document")
    }
}

Everything works fine, and the result looks like this: enter image description here

The problem is, when I set the binding for the Table Cell view, like so: enter image description here

The program still loads up, but when I click Add Emplyee, the program crashes with the error

Xcode Version: 10.1 Swift Version: 4 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) with no other information in the console as to the error.

Genu
  • 1,094
  • 1
  • 9
  • 22

1 Answers1

2

Cocoa Bindings are based on Key Value Observing, the properties must be marked as dynamic and Objective-C inference has been changed in Swift 4, you have to add the @objc attribute to each affected property.

class Employee: NSObject {
   @objc dynamic var name: String? = "New Employee" 
   @objc dynamic var raise: Float = 0.05
}
vadian
  • 274,689
  • 30
  • 353
  • 361