选择器 dataTask(with: completionHandler:) 的使用不明确

ambiguous use of selector dataTask(with: completionHandler:)

我正在尝试获取 URLSession 中定义的 dataTask(with:completionHandler:) 方法的 selector,它使用如下所示的 URLRequest 对象,但出现错误,因为有两个参数名称略有不同的方法(重载方法 - 1. 一个使用 URLRequest 对象作为参数,另一个使用 URL):

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

我尝试了如下不同的方法(在 https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md 中提到),但它也给出了同样的错误:

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

我使用的是最新的 Xcode 和 swift 3。不幸的是,到目前为止我没有找到包含类似示例的好文档。请帮忙。

提前致谢!

你可以这样写selector

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

#selector tutorial 帮助我找到解决方案。