Find below my sample code that shows how it's impossible to associate any custom data with a path item. PathControl.pathItems.didSet is called only once when explicitly setting them in loadView(), and in the action selectPath(_:) the path items are not of my custom subclass PathControlItem but are again NSPathControlItem. They change their address on every click, and finding the clicked path item in the pathItems array always returns nil even if by comparing the logged addresses it appears to be there.
class ViewController: NSViewController {
override func loadView() {
let pathControl = PathControl()
pathControl.action = #selector(selectPath(_:))
pathControl.pathItems = ["a", "b", "c"].map({ title in
let item = PathControlItem()
item.title = title
item.customData = title
return item
})
view = NSStackView(views: [pathControl])
}
@IBAction func selectPath(_ sender: NSPathControl) {
print("click", sender.clickedPathItem!.description, (sender.clickedPathItem as? PathControlItem)?.customData, sender.pathItems.description, sender.pathItems.firstIndex(of: sender.clickedPathItem!), sender.pathItems.firstIndex(where: { $0.description == sender.clickedPathItem!.description }))
}
}
class PathControl: NSPathControl {
override var pathItems: [NSPathControlItem] {
didSet {
print("didSet", pathItems)
}
}
}
class PathControlItem: NSPathControlItem {
var customData = ""
}