“模棱两可地使用'儿童'";尝试在Swift 3.0中使用NSTreeController.arrangedObjects时 [英] "Ambiguous use of 'children'" when trying to use NSTreeController.arrangedObjects in Swift 3.0

查看:137
本文介绍了“模棱两可地使用'儿童'";尝试在Swift 3.0中使用NSTreeController.arrangedObjects时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试向不透明的NSTreeController.arrangedObjects对象发送消息时,在 XCode 8.0/Swift 3.0 中出现Ambiguous use of 'children'错误.

I get an Ambiguous use of 'children' error in XCode 8.0/Swift 3.0 when trying to send a message to the opaque NSTreeController.arrangedObjects object.

这是一个空旷的操场,上面显示了用例:

Here is a bare playground showing the use case :

import AppKit

extension NSTreeController {

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

我想用AnyObject作为与基础ObjC对象的桥梁,我想应该可以通过任何方法调用.

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

Xcode表示已找到两个可以响应儿童"消息的候选对象:Foundation.XMLNodeAppKit.NSTreeNode.

Xcode signals that it found two candidates that could respond to a "children" message: Foundation.XMLNode and AppKit.NSTreeNode.

当然,显而易见的解决方案(广播到NSTreeNode)不起作用,因为arrangedObjects返回的是不透明的代理对象,而不是真正的NSTreeNode

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

关于在Swift 3中应该如何使用NSTreeController.arrangedObjects.children的任何建议?

Any suggestion on how we're supposed to use NSTreeController.arrangedObjects.children in Swift 3 ?

推荐答案

children属性的两个候选者的类型不同:

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]?

这篇关于“模棱两可地使用'儿童'";尝试在Swift 3.0中使用NSTreeController.arrangedObjects时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆