简体   繁体   中英

Illegal NSTableView data source

Currently doing some swift development and am playing with this tutorial: https://www.brandpending.com/2016/01/14/using-core-data-in-a-swift-cocoa-app-to-populate-an-nstableview/

I have updated the code as some of the above code is out of date, however i am getting the dreaded

*** Illegal NSTableView data source

Not sure what is causing the issue as i do not have my TableView delegate or data source outlets linked outside of the custom stuff created in the above tutorial. The code which is throwing the error i believe is in below functions somewhere. Otherwise there must be something i am missing.

 override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let appDelegate = NSApplication.shared().delegate as! AppDelegate
    managedContext = appDelegate.managedObjectContext

    tableView.delegate = self
    tableView.dataSource = self

    fetchDataAndRefreshTable()

}

//MARK: Table Stuff
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
    return data.count
}

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let result = tableView.make(withIdentifier: tableColumn!.identifier, owner: self) as! NSTableCellView

    let item = data[row]

    if let val = item.value(forKey: tableColumn!.identifier) as? String {
        result.textField?.stringValue = val
    } else {
        result.textField?.stringValue = ""
    }
    return result
}

Any help would be greatly appreciated!

The tutorial is written in Swift 2. Your code seems to be Swift 3.

The signature of numberOfRowsInTableView is wrong. In Swift 3 it's

func numberOfRows(in tableView: NSTableView) -> Int {
    return data.count
}

PS: It is preferable to connect delegate and data source in Interface Builder rather than in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM