UIWebView LoadData is not accepting Nil values for

2019-03-11 13:17发布

Till Swift 1.2 version, UIWebView LoadData was accepting nil values, but Swift 2.0 is throwing error "Swift does not conform to protocol NilLateralConvertible".

Swift 1.2: Works fine

self.webView.loadData(tempData!, MIMEType: "application/pdf", textEncodingName: nil, baseURL: nil)

Swift 2.0: Throws error

self.webView.loadData(tempData!, MIMEType: "application/pdf", textEncodingName: nil, baseURL: nil)

4条回答
看我几分像从前
2楼-- · 2019-03-11 13:51

The default character encoding is UTF-8 so you can do in this way:

self.webView.loadData(tempData!, MIMEType: "application/pdf", textEncodingName: "UTF-8", baseURL: NSURL())

查看更多
神经病院院长
3楼-- · 2019-03-11 13:56

Apple has updated the declarations and they now require non-nil values. They have adding reality to the declarations for the benefit of Swift (and ObjC).

- (void)loadData:(NSData   * nonnull)data
        MIMEType:(NSString * nonnull)MIMEType
textEncodingName:(NSString * nonnull)encodingName
         baseURL:(NSURL    * nonnull)baseURL
查看更多
Animai°情兽
4楼-- · 2019-03-11 13:58

Swift 3:

self.webView.loadData(tempData!, MIMEType: "application/pdf", textEncodingName: "UTF-8", baseURL: NSURL() as URL)
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-11 14:07

This works for me:

webView.loadData(pdfDownload, MIMEType: "application/pdf", textEncodingName: "", baseURL: NSURL())
查看更多
登录 后发表回答