SOUA Hamza
SOUA Hamza

Reputation: 146

Ambiguous use of 'connection(_:didReceive:)'

static let didReceiveResponseSelector : Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)) as (NSURLConnectionDataDelegate) ->(NSURLConnection,URLResponse) -> ())

This code is returning error:

Ambiguous use of 'connection(_:didReceive:)'

I referred to the official evolution thread of Apple on GitHub, I respected the syntax but is not working:

Referencing the Objective-C selector of a method

Upvotes: 1

Views: 356

Answers (3)

SOUA Hamza
SOUA Hamza

Reputation: 146

solved, just add "?":

    static let didReceiveResponseSelector : Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)) as ((NSURLConnectionDataDelegate) -> (NSURLConnection,URLResponse) -> void)?)

Upvotes: 0

JAL
JAL

Reputation: 42479

Don't cast the Selector:

let didReceiveResponseSelector = #selector(NSURLConnectionDelegate.connection(_:didReceive:))

It's also worth noting that the delegate function connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge) has been deprecated in favor of connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge).

Upvotes: 0

root
root

Reputation: 229

NSURLConnectionDataDelegate is a protocol, you can't create a Selector using NSURLConnectionDataDelegate.connection(_:didReceive:), you must used an implementation of NSURLConnectionDataDelegate like :

class YourDelegateImplementation: NSURLConnectionDataDelegate {
     public func connection(_ connection: NSURLConnection, didReceive data: Data) {
     }
}

And then you can create a Selector like this :

let yourDelegate: YourDelegateImplementation = YourDelegateImplementation()
let yourSelector : Selector = #selector(yourDelegate.connection(_:didReceive:))

Upvotes: 1

Related Questions