0

I am getting an error while developing an app which downloads data from the Web.

if let url = URL(string: "https://www.stackoverflow.com") {

        let request = NSMutableURLRequest(url: url)

        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in

            if error != nil {
                print(error)
            } else {

                if let unwrappedData = data {

                    let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

                    print(dataString)

                    DispatchQueue.main.sync(execute: {
                    // Update UI

                    })
                }
            }

        }

        task.resume()

    }

I am getting this error (Shown in the image below):

In this image, you can see the error clearly.

Please let me know what's wrong with the code. I am using Swift 3 in Xcode 8 Beta 4.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Captain Cold
  • 179
  • 1
  • 1
  • 3
  • 3
    I'm not sure this is an error. You are just hitting a breakpoint. Click on the blue arrow in the 'gutter' to the left of the code, it'll turn light blue which means it is disabled. Then re-run your app. When you get a chance, try to research about 'debugging using xcode'. – ncke Aug 22 '16 at 21:08
  • 1
    As an aside, rather than `NSMutableURLRequest`, use `URLRequest` directly. Then you don't need to cast it. And if you need it to be mutable, use `var` instead of `let`. Also, there's no need to make the dispatch to the main queue synchronous. You can use `async`. – Rob Aug 22 '16 at 21:08
  • @ncke Thank you. That really helped. – Captain Cold Aug 22 '16 at 21:11
  • @Rob Thank you for a quick reply. – Captain Cold Aug 22 '16 at 21:12
  • You also can do `let dataString = String(data: unwrapped, encoding: .utf8)`. – Rob Aug 22 '16 at 22:58

0 Answers0