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

家住魔仙堡 提交于 2019-12-01 22:10:56

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

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.

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