2

I'm migrating some selectors to Swift 2.2 but I'm having an issue with one of them:

Init string selector

Code:

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

Now, Xcode want to help me doing the transition but in a wrong way:

enter image description here

Code:

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

I try to fix it:

enter image description here

Code:

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

So, my question is: How can I select the default initialiser method with the new #selector syntax?

(It was working in Swift 2.0.)

Current error:

"Ambiguous use of 'init()'"

UPDATE 1:

ARTRealtimePresenceQuery initialisers:

- (instancetype)init;
- (instancetype)initWithClientId:(nullable NSString *)clientId connectionId:(nullable NSString *)connectionId;
- (instancetype)initWithLimit:(NSUInteger)limit clientId:(nullable NSString *)clientId connectionId:(nullable NSString *)connectionId;
ricardopereira
  • 11,118
  • 5
  • 63
  • 81
  • I am not familiar with that framework, what does `testSuite_injectIntoClassMethod` do, and the init method of which class do you want to refer to? – Note that `Selector("init")` is still valid! – Martin R Apr 09 '16 at 14:43
  • @MartinR The `testSuite_injectIntoClassMethod` adds a block of code after the current `selector` for a specific class. In this case, I want to add a block of code in the default initialiser of `ARTRealtimePresenceQuery`. Can I still use the `Selector("init")` without a warning? I think not. – ricardopereira Apr 09 '16 at 15:01
  • 1
    Can you try `#selector(ARTRealtimePresenceQuery.init as () -> ARTRealtimePresenceQuery)` ? It should compile without warning, but don't know if it works as intended. – Martin R Apr 09 '16 at 15:29
  • @MartinR It works! Great, really thank you. Please write an answer to accept it. – ricardopereira Apr 09 '16 at 15:36

1 Answers1

2

As demonstrated in

referring to the no-parameter variant as one of several overloaded methods requires an explicit as <signature> cast. That method can be applied to init methods as well:

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)

So in your case it would be

#selector(ARTRealtimePresenceQuery.init as () -> ARTRealtimePresenceQuery)
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382