1
votes

I keep receiving the same error of: Cannot invoke loadData with an argument of list type '(NSData, MIMEType: String, textEncodingName: nil, baseURL: nil)'

for the loadData method.

var filePath = NSBundle.mainBundle().pathForResource("fractal", ofType: "gif")

var gif = NSData(contentsOfFile: filePath!)

var webViewBG = UIWebView(frame: self.view.frame)

webViewBG.loadData(gif!,MIMEType: "image/gif",textEncodingName: nil,baseURL: nil) // this line of code causes the build error
1

1 Answers

1
votes

You should check loadData function signature, which is:

func loadData(_ data: NSData, MIMEType MIMEType: String,
  textEncodingName textEncodingName: String, baseURL baseURL: NSURL)

textEncodingName is String, not String?, thus you can't pass nil. Same applies to baseURL where the type is NSURL, not NSURL?.

In this case, you can pass whatever values like utf-8 and http://localhost/ to meet non-nil criteria.

Check this thread for other ways how to do it.

Try to minimize usage of ! to avoid runtime failures. Something like this is much more robust:

guard let filePath = NSBundle.mainBundle().pathForResource("fractal", ofType: "gif"),
  let gifData = NSData(contentsOfFile: filePath) else {
    return
}

var webViewBG = UIWebView(frame: self.view.frame)
webViewBG.loadData(gifData, MIMEType: "image/gif", textEncodingName: "utf-8",
  baseURL: NSURL(string: "http://localhost/")!)