static let didReceiveResponseSelector : Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)) as (NSURLConnectionDataDelegate) ->(NSURLConnection,URLResponse) -> ())
此代码返回错误:
"connection(_:didReceive:("的模棱两可的使用
我在 GitHub 上提到了 Apple 的官方进化线程,我尊重语法但不起作用:
引用方法的 Objective-C 选择器
NSURLConnectionDataDelegate
是一个协议,你不能使用NSURLConnectionDataDelegate.connection(_:didReceive:)
创建选择器,你必须使用NSURLConnectionDataDelegate的实现,如下所示:
class YourDelegateImplementation: NSURLConnectionDataDelegate {
public func connection(_ connection: NSURLConnection, didReceive data: Data) {
}
}
然后你可以创建一个这样的选择器:
let yourDelegate: YourDelegateImplementation = YourDelegateImplementation()
let yourSelector : Selector = #selector(yourDelegate.connection(_:didReceive:))
不要强制转换选择器:
let didReceiveResponseSelector = #selector(NSURLConnectionDelegate.connection(_:didReceive:))
还值得注意的是,委托函数connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge)
已被弃用,取而代之的是connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge)
。
解决了,只需添加"?":
static let didReceiveResponseSelector : Selector = #selector((NSURLConnectionDataDelegate.connection(_:didReceive:)) as ((NSURLConnectionDataDelegate) -> (NSURLConnection,URLResponse) -> void)?)