If I wanted to do a table view data source in ObjC, it would look like this:
- tableView:aTableView objectValueForTableColumn:aColumn row:(NSInteger)row {
return [representedObject.collection[row] valueForKey:[aColumn identifier]];
}
When I do it in Swift, it ends up looking like this:
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
guard let identifier = tableColumn?.identifier else {
assertionFailure("No table column")
return nil
}
guard let obj = (self.representedObject as? ModelType)?.collection(at:row) else {
assertionFailure("Can't find model object at \(row)")
return nil
}
switch identifier {
case NSUserInterfaceItemIdentifier(rawValue:"column1"):
return obj.field1
case NSUserInterfaceItemIdentifier(rawValue:"column2"):
return objc.field2
//...
default:
assertionFailure("Unknown table column \(tableColumn?.identifier ?? NSUserInterfaceItemIdentifier(rawValue: "unknown"))")
return nil
}
}
I can’t help feeling I’m doing it wrong.