Swift 4 / Xcode 10错误消息:非法NSOutlineView数据源

问题描述 投票:2回答:1

我正在为项目使用Xcode 10和Swift 4。

我正在使用IB创建自己的viewController,以便在项目中集成自定义视图时将它们集成在一起。

在这些视图控制器之一上,我正在使用NSOutlineView来显示网络设备列表。

    public class applianceListPickerViewController: NSViewController, NSOutlineViewDelegate, NSOutlineViewDataSource {




@IBOutlet weak var applianceTreeView: NSOutlineView!
private var listOfAppliances = [appliance]()

override public func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

        self.applianceTreeView.dataSource = self
        self.applianceTreeView.delegate = self
        self.readRouterListFromFile(fileName: "lab_list")
        self.applianceTreeView.reloadData()
}

func readRouterListFromFile(fileName: String) -> Any?
{
    var listOfRouters = [BSDRouters]()
    if let path = Bundle.main.path(forResource: fileName, ofType: "json") {
        do {
            let fileUrl = URL(fileURLWithPath: path)
            // Getting data from JSON file using the file URL

            let data = try Data(contentsOf: fileUrl, options: .mappedIfSafe)
            listOfRouters = try JSONDecoder().decode([BSDRouters].self, from: data)

        } catch {
            // Handle error here
            print ("Not a valid Router list \(error)")
            return nil
        }
    }
    else{
        return nil
    }
    for currentRouter in listOfRouters {
        let anAppliance = appliance.init(withName: currentRouter.name)
        self.listOfAppliances.append(anAppliance    )
    }
    return listOfRouters
}

// MARK: - Data Source functions


// This one returns the appliances count if item is an appliance and pipe counts if its a pipe

private func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
    if let currentItem = item as? appliance {
        return currentItem.listOfipes.count
    }
    return listOfAppliances.count

}


private func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any?{
    print("Don't know why I have to define this one but its in the error message so I declare it")
    return nil
}

// This one returns an appliance object if item is an appliance and a pipe object  if its a pipe

private func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
    if let currentItem = item as? appliance {
        return currentItem.listOfipes[index]
    }

    return listOfAppliances[index]
}

private func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
    if let currentItem = item as? appliance {
        return currentItem.listOfipes.count > 0
    }
    return false
}

// MARK: - Delegates functions functions


private func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
    var view: NSTableCellView?
    if let currentItem = item as? appliance {
        view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "routerCell"), owner: self) as? NSTableCellView
        if let textField = view?.textField {
            textField.stringValue = currentItem.name
            textField.sizeToFit()
        }
    }
    return view
}}

我在调试部分中收到以下错误消息:

非法NSOutlineView数据源()。必须实现outlineView:numberOfChildrenOfItem:,outlineView:isItemExpandable:,outlineView:child:ofItem:和outlineView:objectValueForTableColumn:byItem:

不幸的是,我已经定义了方法!

在阅读文档时说,可选方法outlineView(_ outlineView:NSOutlineView,objectValueFor tableColumn:NSTableColumn ?, byItem项目:任何?)->任何吗?当不使用Cocoa绑定时,这是强制性的,因为我手动设置数据源和委托。

我也一直在看:Error: Illegal NSOutlineView data source,但其中没有任何线索可以解决我的问题。

在IB上,我已将插座ApplianceTreeView链接到我的xib的OutlineView。

我已经尝试在viewdidload之后手动设置deviceTreeView的委托和数据源,因为nsoutlineview似乎不符合委托和数据源的协议,但仍然没有成功,但仍然得到相同的错误消息。

swift xcode nsoutlineview
1个回答
1
投票

删除private

// This one returns the appliances count if item is an appliance and pipe counts if its a pipe

 func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
    if let currentItem = item as? appliance {
        return currentItem.listOfipes.count
    }
    return listOfAppliances.count

}


 func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any?{
    print("Don't know why I have to define this one but its in the error message so I declare it")
    return nil
}
© www.soinside.com 2019 - 2024. All rights reserved.