模糊地使用'connection(_:didReceive :)'

时间:2017-05-12 15:41:42

标签: swift swift3 nsurlconnectiondelegate xcode8

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

此代码返回错误:

  

模糊地使用'connection(_:didReceive :)'

我在GitHub上提到了Apple的官方发展主题,我尊重语法但是没有用:

Referencing the Objective-C selector of a method

3 个答案:

答案 0 :(得分:1)

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:))

答案 1 :(得分:0)

不要施放选择器:

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

还值得注意的是,委托函数connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge)已被弃用,而不是connection(_ connection: NSURLConnection, willSendRequestFor challenge: URLAuthenticationChallenge)

答案 2 :(得分:0)

解决了,只需添加“?”:

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