Error: Illegal NSOutlineView data source

你。 提交于 2020-05-30 07:58:09

问题


I created a prototype that successfully implements a Source View based on this tutorial. I then attempted to port it to the real application. When I run it, I get the error

2016-12-17 18:30:43.693194 jazzcat[67629:1970219] *** Illegal NSOutlineView data source (). Must implement outlineView:numberOfChildrenOfItem:, outlineView:isItemExpandable:, outlineView:child:ofItem: and outlineView:objectValueForTableColumn:byItem:

I've spent hours comparing the two applications and can't find a difference especially since I copied and pasted the code. Another question here about a similar problem had a response that said to print out the object id of the view controller. The example was for Objective C. How do you do that in Swift?

This is my controller:

import Cocoa

class ViewController: NSViewController {

  var managedContext = (NSApplication.shared().delegate as! AppDelegate).managedObjectContext
  var items = [String]()

  override func viewDidLoad() {
    super.viewDidLoad()
    print("viewDidLoad")

    items = Catalog.entryList()
  }

  override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
   }
  }
}

extension ViewController: NSOutlineViewDataSource {
  func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  return Catalog.entryCount()
}

func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  return items[index]
}

func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  return false
}

func outlineViewSelectionDidChange(_ notification: Notification) {
  guard let outlineView = notification.object as? NSOutlineView else {
    return
  }
  let selectedIndex = outlineView.selectedRow
 }
/*  func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn:  NSTableColumn?,
               byItem item: Any?) -> Any? {
  return nil
}*/
}

extension ViewController: NSOutlineViewDelegate {
  func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  var view: NSTableCellView?

  view = outlineView.make(withIdentifier: "HeaderCell", owner: self) as? NSTableCellView
  if let textField = view?.textField {
    textField.stringValue = String(describing: item)
  }
  return view
  }
}

The commented out function objectValueFor is not in the working version. I tried it here because it was one of the functions mentioned in the error. Including it didn't fix the problem.

I checked the datasource and delegate mapping in the storyboard and redid it several times to be sure it was right. I cross checked many of the other setting to be sure they were the same. One difference is that the application has the line of code setting the managed context. It's for another control. Commenting it out just causes another error. Any suggestions at what else to look at?

来源:https://stackoverflow.com/questions/41205059/error-illegal-nsoutlineview-data-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!