-2

我想创建一个 UITableview。我找到了一个示例项目,但它适用于 Swift 3。我正在使用 Swift 5.x。我几乎可以删除所有错误,但剩下 1 个,我不知道如何解决它。这是代码

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate,NSTableViewDataSource,NSTableViewDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var myTableView: NSTableView!

    var dataArray:[NSMutableDictionary] = [["firstName": "Debasis", "lastName": "Das", "fullTimeEmp": 0],
        ["firstName": "Nishant", "lastName": "Singh", "fullTimeEmp": 1],
        ["firstName": "John", "lastName": "Doe", "fullTimeEmp": 1],
        ["firstName": "Jane", "lastName": "Doe", "fullTimeEmp": 1],
        ["firstName": "Mary", "lastName": "Jane", "fullTimeEmp": 0]];

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
    {
        let numberOfRows:Int = dataArray.count
        return numberOfRows
    }

    private func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject!
    {
        let object = dataArray[row] as NSMutableDictionary
        if ((tableColumn.identifier).rawValue == "fullTimeEmp")
        {
            return object[tableColumn.identifier] as? Int? as AnyObject
        }
        else
        {
            return object[tableColumn.identifier] as? String? as AnyObject
        }
    }
    
    private func tableView(tableView: NSTableView, setObjectValue object: AnyObject?, forTableColumn tableColumn: NSTableColumn?, row: Int)
    {
        dataArray[row].setObject(object!, forKey: (tableColumn?.identifier)!)
    }


}

错误

任何想法如何解决它?

4

1 回答 1

0

作为对您发布的错误的快速回答:

Swift 不能推断出你复杂代码的类型,所以你应该用这个来代替它来帮助它:

dataArray[row].setObject(object!, forKey: tableColumn!.identifier as NSCopying)

但!

您的代码有很多问题,例如:

  • 不安全的力量展开
  • 不确定的展开 (x?.y)!
  • appdelegate 不相关的用法。
  • 等等

尝试更多文章和教程并逐步监控您的进度;)

于 2020-12-18T13:22:01.740 回答