bogdanf bogdanf - 1 year ago 125
Swift Question

"Ambiguous use of 'children'" when trying to use NSTreeController.arrangedObjects in Swift 3.0

I get an

Ambiguous use of 'children'
error in XCode 8.0/Swift 3.0 when trying to send a message to the opaque

Here is a bare playground showing the use case :

import AppKit

extension NSTreeController {

func whatever () {
let k = (self.arrangedObjects as AnyObject).children // error here

I try to use
as a bridge to the underlying ObjC object, which is supposed to be able to get through any method call, I guess.

Xcode signals that it found two candidates that could respond to a "children" message:

Of course the obvious solution (casting to
) is not working because
returns an opaque, proxy object not a real

Any suggestion on how we're supposed to use
in Swift 3 ?

Answer Source

The two candidates for the children property differ by their type:

Foundation.XMLNode:137:14: note: found this candidate
    open var children: [XMLNode]? { get }
AppKit.NSTreeNode:12:14: note: found this candidate
    open var children: [NSTreeNode]? { get }

You can resolve the ambiguity by casting the value of the property to the expected type. In your case:

let k = (self.arrangedObjects as AnyObject).children as [NSTreeNode]?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download