简体   繁体   English

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

[英]Swift 4 / Xcode 10 Error message : Illegal NSOutlineView data source

***** SOLUTION THAT WORKED FOR ME ****** ***** 对我有用的解决方案******

Quit and restart Xcode fixed the issue退出并重新启动 Xcode 修复了问题


I'm using Xcode 10 and swift 4 for my project.我的项目使用 Xcode 10 和 swift 4。

I'm creating my own viewControllers using IB to have them integrated if custom views in my project.我正在使用 IB 创建我自己的视图控制器,以便在我的项目中集成自定义视图。

On one of these view controllers I'm using an NSOutlineView to display a list of network appliances.在这些视图控制器之一上,我使用 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
}}

I'm getting the following error message in the debug section:我在调试部分收到以下错误消息:

Illegal NSOutlineView data source ().非法的 NSOutlineView 数据源 ()。 Must implement outlineView:numberOfChildrenOfItem:, outlineView:isItemExpandable:, outlineView:child:ofItem: and outlineView:objectValueForTableColumn:byItem:必须实现 outlineView:numberOfChildrenOfItem:、outlineView:isItemExpandable:、outlineView:child:ofItem: 和 outlineView:objectValueForTableColumn:byItem:

Which is unfortunate as I have defined the methods!这是不幸的,因为我已经定义了方法!

Reading at the documentation it says that optional method outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any?在文档中阅读它说可选方法 outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? is mandatory when not using Cocoa bindings which I do as I manually set the datasource and delegate.当我手动设置数据源和委托时,不使用 Cocoa 绑定时是强制性的。

I've been also looking at this : Error: Illegal NSOutlineView data source but it doesn't contain any leads to resolve my issue.我也一直在看这个: 错误:非法的 NSOutlineView 数据源,但它不包含任何解决我的问题的线索。

On the IB I've linked the outlet applianceTreeView to the OutlineView of my xib.在 IB 上,我已将插座 ApplianceTreeView 链接到我的 xib 的 OutlineView。

I've tried to set the delegate and the datasource of the applianceTreeView manually after the viewdidload as it appears that the nsoutlineview doesn't conform to the protocols for delegate and datasource but without success still getting the same error message.我尝试在 viewdidload 之后手动设置 ApplianceTreeView 的委托和数据源,因为看起来 nsoutlineview 不符合委托和数据源的协议,但没有成功仍然收到相同的错误消息。

Editing :编辑:

Maybe something is wrong in the datasource and delegate assignment as I can see that the datasource and delegate are not pointing to the viewcontroler (self 0x00006000000e3300) but to the outlineview (outlineview 0x0000000101603e40) as shown in the image below :也许数据源和委托分配有问题,因为我可以看到数据源和委托不是指向视图控制器(自我 0x00006000000e3300)而是指向大纲视图(大纲视图 0x0000000101603e40),如下图所示: 在此处输入图片说明

Replace private with publicprivate替换为public

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

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

}


public 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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