“模棱两可地使用'儿童'";尝试在Swift 3.0中使用NSTreeController.arrangedObjects时 [英] "Ambiguous use of 'children'" when trying to use NSTreeController.arrangedObjects in Swift 3.0
问题描述
当尝试向不透明的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.XMLNode
和AppKit.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屋!