I'm trying to bind an NSTextView
to an NSArrayController
in code, and have followed a few questions already asked on the subject: here, here, and here. I'm building a MacOS app using Xcode 9.2 and Swift 4.
I have a SegmentedControl
with 2 buttons: one to add a new object and the other to remove. I'm crashing when I try to bind the a NSTextField
to a newly created object added to the array.
My code for the VeiwController
:
private enum Columns {
static let name = NSUserInterfaceItemIdentifier("NameColumn")
static let age = NSUserInterfaceItemIdentifier("AgeColumn")
}
let arrayController: NSArrayController
let table: NSTableView
let buttons: NSSegmentedControl
// MARK: —Initialisation—
required init() {
super.init(nibName: nil, bundle: nil)
/// Code to create arrayController, table, and buttons
///...
/// Bindings.
table.bind(.content,
to: self.arrayController,
withKeyPath: "arrangedObjects",
options: nil)
/// Delegates
table.delegate = self
buttons.target = self
buttons.action = #selector(splitButtonClicked)
}
The function to add/remove objects from the arrayController
. Person
is a simple class with 2 properties (Name
and Age
):
@objc func splitButtonClicked() {
switch buttons.selectedSegment {
case 0:
let person = Person()
person.name = "Alfred"
person.age = 21
arrayController.addObject(person)
case 1:
arrayController.removeObject(arrayController.selectedObjects)
default:
print("Switch case error")
}
}
The code to create a new table row returns a new NSTextField
, but I'm crashing when I try to bind the text field and I'm not sure why:
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let column = tableColumn else { return nil }
let t = NSTextField(frame: NSRect(x: 0, y: 0, width: column.width, height: table.rowHeight))
if column.identifier == Columns.name {
/// Crashing on this line.
t.bind(.value, to: t, withKeyPath: "objectValue.name", options: nil)
} else if column.identifier == Columns.age {
t.bind(.value, to: t, withKeyPath: "objectValue.age", options: nil)
}
return t
}