[英]swift 3 WKWebView delegate WKNavigationDelegate Ambiguous method
I created a class named JSBridge
, this class implements the WKNavigationDelegate
, I did some custom stuff in the protocol method, and then call another UIViewController
's default WKNavigationDelegate
, for example WebViewController
aka webViewDelegate
, so I have to do like this我创建了一个名为JSBridge
的类,这个类实现了WKNavigationDelegate
,我在协议方法中做了一些自定义的东西,然后调用另一个UIViewController
的默认WKNavigationDelegate
,例如WebViewController
aka webViewDelegate
,所以我必须这样做
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
//do something here then call `WebViewController`'s implementation
if webViewDelegate.responds(to: #selector(webViewDelegate.webView(_:decidePolicyFor:decisionHandler:))) {
webViewDelegate.webView!(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
} else {
decisionHandler(.allow)
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
//do something here then
if webViewDelegate.responds(to: #selector(webViewDelegate.webView(_:decidePolicyFor:decisionHandler:))) {
webViewDelegate.webView!(webView, decidePolicyFor: navigationResponse, decisionHandler: decisionHandler)
} else {
decisionHandler(.allow)
}
}
my problem is in xcode8(swift3) the compiler gives me the error: Ambiguous use of 'webView(_:decidePolicyFor:decisionHandler:)'
, they have the same name to compiler我的问题是在 xcode8(swift3) 中,编译器给了我错误: Ambiguous use of 'webView(_:decidePolicyFor:decisionHandler:)'
,它们与编译器具有相同的名称
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
You can use as
to choose one from some ambiguous methods:您可以使用as
从一些模棱两可的方法中选择一种:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
typealias WKNavigationActionMethodType = (WKWebView,WKNavigationAction,@escaping(WKNavigationActionPolicy)->Void) -> Void
if webViewDelegate.responds(to: #selector(webView(_:decidePolicyFor:decisionHandler:) as WKNavigationActionMethodType)) {
webViewDelegate.webView!(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
} else {
decisionHandler(.allow)
}
}
Another way to treat this sort of situation is "avoid using responds(to:)
and use Optional chaining":处理这种情况的另一种方法是“避免使用responds(to:)
并使用 Optional chaining”:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let webViewDelegate = webViewDelegate , self.webView == webView else {
decisionHandler(.allow)
return
}
if webViewDelegate.webView?(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler) == nil {
decisionHandler(.allow)
}
}
The delegate method's return type is Void
, so its Optional chained result is of type Void?
委托方法的返回类型是Void
,所以它的 Optional 链接结果是Void?
类型Void?
. . In your case, it becomes nil
only when the optional method is not implemented in the instance.在您的情况下,仅当实例中未实现可选方法时它才变为nil
。 So, comparing the result to nil
is checking the optional method is implemented (and actually called) or not.因此,将结果与nil
进行比较是检查可选方法是否已实现(并实际调用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.