1

I am trying to load HTML content from this website onto a Webview. But I am getting the following error:

Cannot convert value of type 'NSString?' to expected argument type 'String'

Here is my code:

override func viewDidLoad() {
        super.viewDidLoad()

        let webUrl = NSURL(string: "http://www.stackoverflow.com/users/5438240/naishta")

        let webContent = NSURLSession.sharedSession().dataTaskWithURL(webUrl!){
            (data,response,error) in

            if error == nil{

                let readableForm = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print(readableForm)

                self.displayWeb.loadHTMLString(readableForm, baseURL: nil) //here is where the error is thrown
            }


        }
        webContent.resume()

    }

3 Answers 3

3

You should use Swift native type String and use guard to make sure data it is not nil as follow:

guard 
    let data = data where error == nil, readableForm = String(data: data, encoding: NSUTF8StringEncoding)
else { return }

print(readableForm)
self.displayWeb.loadHTMLString(readableForm, baseURL: nil)
Sign up to request clarification or add additional context in comments.

Comments

1

Was able to resolve myself with the change as below, needed a "as! String" conversion

 self.displayWeb.loadHTMLString(readableForm as! String, baseURL: nil)

Comments

-1

The actual way is

var a:NSString = "this is"
let c = String(a)

1 Comment

@EricAya Can you show me a Apple Doc which tells developer to use without __.init() ? Just to make it a fact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.