无法使用 'NSUserInterfaceItemIdentifier' 类型的索引下标“[String : String]”类型的值

Cannot subscript a value of type '[String : String]' with an index of type 'NSUserInterfaceItemIdentifier'

在我的项目(MacOS,Swift 4)中,当我尝试设置 NSTableViewDataSource 时出现此错误:"Cannot subscript a value of type '[String : String]' with an index of type 'NSUserInterfaceItemIdentifier'"。 它在 Swift 3 中工作得很好,但是当我将项目更新到 Swift 4 时,它停止工作了。

所以我要做的是使用单元格 ID 在 "tableView" 中显示 "tableViewData"。

这是我的代码:

    import Cocoa

    class ViewController: NSViewController {

        @IBOutlet weak var tableView:NSTableView!

        let tableViewData = [["picID":"default","nameID":"default","amount":"1","address":"default"],["picID":"default","nameID":"default","amount":"1","address":"default"]]

        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.delegate = self as? NSTableViewDelegate
            self.tableView.dataSource = self


        }
    }

    extension ViewController:NSTableViewDataSource{
        func numberOfRows(in tableView: NSTableView) -> Int {
            return tableViewData.count
        }

        func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
            return tableViewData[row][(tableColumn?.identifier)!] // This is where the error appears 
        }
    }

知道 Swift 4 中发生了什么变化吗?

NSUserInterfaceItemIdentifier 是结构类型。您应该调用 .rawValue 以获取基础值:

tableViewData[row][(tableColumn?.identifier.rawValue)!]