9
votes

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
Any leads? Get warnings when using Swift 3+ with "NSURL() as URL"Sunkas

4 Answers

14
votes

This works for me:

webView.loadData(pdfDownload, MIMEType: "application/pdf", textEncodingName: "", baseURL: NSURL())
3
votes

Swift 3:

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

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

1
votes

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