Xcode 7.3,Swift 2.2:默认初始化选择器(新编译时检查)

时间:2016-04-09 14:22:00

标签: swift

我正在将一些选择器迁移到Swift 2.2但我遇到其中一个问题:

Init string selector

代码:

let hook = ARTRealtimePresenceQuery.testSuite_injectIntoClassMethod("init") { // Default initialiser
    presenceQueryWasCreated = true
}

现在,Xcode希望帮助我进行转换,但方式错误:

enter image description here

代码:

let hook = ARTRealtimePresenceQuery.testSuite_injectIntoClassMethod(#selector(_NSEnumeratorType.init)) { // Default initialiser
    presenceQueryWasCreated = true
}

我尝试修复它:

enter image description here

代码:

let hook = ARTRealtimePresenceQuery.testSuite_injectIntoClassMethod(#selector(ARTRealtimePresenceQuery.init)) { // Default initialiser
    presenceQueryWasCreated = true
}

所以,我的问题是:如何使用新的#selector语法选择默认的初始化方法?

(它在Swift 2.0中运行。)

当前错误

不明确地使用'init()'

更新1:

ARTRealtimePresenceQuery初始化者:

- (instancetype)init;
- (instancetype)initWithClientId:(nullable NSString *)clientId connectionId:(nullable NSString *)connectionId;
- (instancetype)initWithLimit:(NSUInteger)limit clientId:(nullable NSString *)clientId connectionId:(nullable NSString *)connectionId;

1 个答案:

答案 0 :(得分:2)

中所示

将无参数变量称为多个重载之一 方法需要明确的as <signature>强制转换。 该方法也可以应用于init方法:

class MyClass : NSObject {

    override init() { }

    init(s : String) { }
}

// Selector referring to init():
let sel1 = #selector(MyClass.init as () -> MyClass)

// Selector referring to init(s : String):
let sel2 = #selector(MyClass.init as (String) -> MyClass)

所以在你的情况下,它将是

#selector(ARTRealtimePresenceQuery.init as () -> ARTRealtimePresenceQuery)