ambiguous use of selector dataTask(with: completionHandler:)

泄露秘密 提交于 2019-12-21 18:00:30

问题


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)

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!


回答1:


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.



来源:https://stackoverflow.com/questions/41545512/ambiguous-use-of-selector-datataskwith-completionhandler

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