0

I'm getting errors on the following lines...

let taskData = session.dataTaskWithRequest(request, completionHandler: { ( data: NSData!, reponse:NSURLResponse!, error: NSError!) -> Void in

let parsedStores = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as! NSDictionary

if let stores: AnyObjec = parsedStores["Stores"]


This code would work for me on IOS8, I converted the syntax to the latest Swift Syntax and it left me with these errors, I've tried to specifically target the area of the errors on the Syntax editor, but it still doesn't fix it. Please help me get rid of these. Thanks

Code for the function.

func loadRecords()
    {

        let urlString = "http://google.com"

        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: config, delegate: nil, delegateQueue: nil)


      //Create the URL Object 
       if let url = NSURL(string: urlString)
        {
            // Create the request object 
            let request = NSURLRequest(URL: url)

             // execute the request 
            let taskData = session.dataTaskWithRequest(request, completionHandler: { ( data: NSData!, reponse:NSURLResponse!, error: NSError!) -> Void in
                // Do something with the data back 
                if (data != nil)
                {
                    //get some data back 
                    print("\(data)")

                    var parseError:NSError?

                    let parsedStores = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as! NSDictionary


                    println("JSON Data \n \(parsedStores)")
                    if let stores: AnyObjec = parsedStores["Stores"]
                    {
                        self.parseJSON(stores)
                    }

                } else

                {    //we got an error
                    print("Error getting stores: \(error.localizedDescription)")


                }

            })

            taskData.resume()
Katz
  • 826
  • 3
  • 19
  • 40

2 Answers2

1

The method signature has changed, it now accepts optionals instead of forcefully unwrapped objects:

func dataTaskWithRequest(_ request: NSURLRequest,
   completionHandler completionHandler: (NSData?,
                              NSURLResponse?,
                              NSError?) -> Void) -> NSURLSessionDataTask

That should fix your error, but you can read more here.

tylersimko
  • 193
  • 7
  • thank you that did fix the error for that particular line, I also have an error on the `let parsedStores = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as! NSDictionary` line saying *extra argument in line* – Katz Nov 21 '15 at 06:32
  • The signature for this method has changed as well. Check the answer to this post: http://stackoverflow.com/questions/31073497/swift-extra-argument-error-in-call – tylersimko Nov 21 '15 at 06:33
1

First off in the new swift those values are optionals and you don't have to specify them as NSData or NSError or NSResponse.

Second you have to do a do this for the JSONObjectWithData as it has throws which is new way of error handling. So you would need to use the new Do, try and catch.

   do {
   let parsedStores = try JSONSerialization.JSONObjectWithData(data, options: nil)
   //Your other code
   }
   catch {
   }
mn1
  • 519
  • 1
  • 5
  • 15