4

I'm trying to get a selector of the dataTask(with:completionHandler:) method defined in URLSession which uses URLRequest object like below, but getting error as there are two methods with slightly two different params names (overloaded methods - 1. one uses URLRequest object as param and another uses URL) :

let dataTaskSelector = #selector(URLSession.dataTask(with: completionHandler:))

I have tried a different approach like below (mentioned in https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md ) but it's also giving same error :

let mySelector = #selector((URLSession.dataTask(with: completionHandler:)) as (URLSession) -> (URLRequest, (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask)

enter image description here

I'm using latest Xcode and swift 3. I didn't find good documentation with similar example on this unfortunately so far. Please help.

Thanks in advance!

Tushar
  • 3,022
  • 2
  • 26
  • 26
  • 1
    @vadian, Sorry to inform you that the question is different here and I didn't find any answer about how to create a selector for this method without this error. – Tushar Jan 09 '17 at 10:24
  • 1
    @Sri, The question is different here. you were too quick to point out that link. I have already gone through that before posting this. – Tushar Jan 09 '17 at 10:25
  • I reopened the question. Why don't you create a custom selector/method which calls the appropriate method of `URLSession`? – vadian Jan 09 '17 at 10:27
  • @vadian, hmm...that would be a way to bypass the original problem. I need the correct Swift technique to create selectors. – Tushar Jan 09 '17 at 10:30
  • Standard selector is `#selector(methodName)` or with a parameter `#selector(methodName(_:))` – vadian Jan 09 '17 at 10:31
  • 1
    @NiravD As you can see I've already reopened the question. – vadian Jan 09 '17 at 10:34
  • @vadian Thanks for reopening it. – Nirav D Jan 09 '17 at 10:35
  • @vadian, you are right.. I have tried the standard selector syntax already but this is a case of Swift's new method naming convention and I'm not able to find correct way to make the selector without this error. – Tushar Jan 09 '17 at 10:37
  • Might be help with This : http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet/40542471#40542471 – Saumil Shah Jan 09 '17 at 10:41
  • @Saumil, unfortunately the problem is different here than what your link has solution for. But thanks a lot for replying – Tushar Jan 09 '17 at 10:44
  • Possible duplicate of [Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug)](http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet) – haider_kazal Jan 09 '17 at 13:58

1 Answers1

4

You can write that selector like this way.

let selector = #selector((URLSession.dataTask(with:completionHandler:)) as (URLSession) -> (URLRequest, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask)

This #selector tutorial help me to get the solution.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Great !!! Thanks a ton Nirav :) I was trying with the syntax very close to you but missed the word "@escaping". Also can you change the syntax to use URLRequest instead of URL to match with the exact question. Doesn't matter for me though.. I got what I needed .. thanks again :) – Tushar Jan 09 '17 at 11:31
  • @Tushar Welcome mate and edited answer for `URLRequest`:) – Nirav D Jan 09 '17 at 11:35
  • So people don't miss it, You need to cast it as ```as ((MyClass) -> (MyMethodType))?``` not just ```as (MyMethodType)``` – Tylerc230 Feb 10 '17 at 19:05
  • @Tylerc230 Yes, But here (MyMethodType) is methods parameter list separated by `,`. – Nirav D Feb 11 '17 at 04:04
  • Error in Xcode 11.5: `Cannot convert value of type '(URLSession) -> (URL, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask' to type '(URLSession) -> (URLRequest, @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask' in coercion` – rraallvv Aug 27 '20 at 01:37