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

南楼画角 提交于 2019-12-02 01:30:41

问题


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

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 signals that it found two candidates that could respond to a "children" message: Foundation.XMLNode and AppKit.NSTreeNode.

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

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


回答1:


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



回答2:


Another solution: Adding an Obj-C category to NSTreeController

.h

#import <Cocoa/Cocoa.h>

@interface NSTreeController (RootNodes_m)

- (NSArray *) rootNodes;

@end

.m

#import "NSTreeController+RootNodes_m.h"

@implementation NSTreeController (RootNodes_m)

- (NSArray *) rootNodes {
    NSObject *  arranged = self.arrangedObjects;

    if ([arranged respondsToSelector: @selector(childNodes)]) {
        return [arranged performSelector:@selector(childNodes)];
    }
    return nil;
}

@end

Now in your code you can use it like this:

return treeController.rootNodes() as? [NSTreeNode]

I had problem with the above answer: The compiler refused to compile when "whole module optimization" was turned on. A swift extension didn't help. I'm using Xcode 8.2.1.



来源:https://stackoverflow.com/questions/39660939/ambiguous-use-of-children-when-trying-to-use-nstreecontroller-arrangedobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!