Shane Lian
Shane Lian

Reputation: 21

iOS 9(Swift 2.0): cannot invoke 'dataTaskwithURL' with an argument list of type '(NSURL, (_, _,_)throws -> Void)'

My app works perfectly fine with iOS 8 but yesterday I upgraded to iOS 9 and Xcode 7 then my app crashes. The error message is cannot invoke 'dataTaskwithURL' with an argument list of type '(NSURL, (_, ,)throws -> Void)'. I googled it and found a similar question here but the solution didn't really work (the solution was to add the do/catch blocks around the code). Can anyone help me with mine problem? Thank you!!!

Here's my code

import UIKit
import Foundation
import CoreLocation

class GoogleDataProvider {

    let apiKey = "AIzaSyCQo-clIkek87N99RVh2lmFX9Mu9QPhAtA"
    let serverKey = "AIzaSyBzmv7wPFcPAe1ucy5o6dqaXnda9i9MqjE"
    var photoCache = [String:UIImage]()
    var placesTask = NSURLSessionDataTask()
    var session: NSURLSession {
    return NSURLSession.sharedSession()
}
    func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius:Double, types:[String],keyword:String, completion: (([GooglePlace]) -> Void)) -> ()
 {
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(serverKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&keyword=\(keyword)&rankby=prominence&sensor=true"
    let typesString = types.count > 0 ? types.joinWithSeparator("|") : "food"
    urlString += "&types=\(typesString)"
    urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

    if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
    placesTask.cancel()
  }

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

do{

 //******************Here's the line that displays error

placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {
    (data, response, error) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  var placesArray = [GooglePlace]()
  if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? NSDictionary {
    if let results = json["results"] as? NSArray {  
      for rawPlace:AnyObject in results {
        let place = GooglePlace(dictionary: rawPlace as! NSDictionary, acceptedTypes: types)
        placesArray.append(place)
        if let reference = place.photoReference {
          self.fetchPhotoFromReference(reference) { image in
            place.photo = image
          }
        }
      }
    }
  }
  dispatch_async(dispatch_get_main_queue()) {
    completion(placesArray)
  }
}
    }catch{

}
    placesTask.resume()
}

    func fetchDirectionsFrom(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D, completion: ((String?) -> Void)) -> ()
 {
    let urlString = "https://maps.googleapis.com/maps/api/directions/json?key=\(serverKey)&origin=\(from.latitude),\(from.longitude)&destination=\(to.latitude),\(to.longitude)&mode=walking"

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

do{
//******************************Here too ****************************
    session.dataTaskWithURL(NSURL(string: urlString)!) {
    (data, response, error) -> Void in
  UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  var encodedRoute: String?
  if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject] {
    if let routes = json["routes"] as AnyObject? as? [AnyObject] {
      if let route = routes.first as? [String : AnyObject] {
        if let polyline = route["overview_polyline"] as AnyObject? as? [String : String] {
          if let points = polyline["points"] as AnyObject? as? String {
            encodedRoute = points
          }
        }
      }
    }
  }
  dispatch_async(dispatch_get_main_queue()) {
    completion(encodedRoute)
  }
}.resume()
}catch{

    }
  }
}  

Sorry this is my first time posting the code style is a little bit confusing sorry about the indentation mess :)

Thanks again!!!

Upvotes: 1

Views: 688

Answers (1)

tounaobun
tounaobun

Reputation: 14857

dataTaskWithURL:completionHandler: does not throw error.

Put do and catch inside dataTaskWithURL method.

for example:

    session.dataTaskWithURL(NSURL(string: urlString)!) {
        (data, response, error) -> Void in
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        var encodedRoute: String?
        do {
            if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject] {
                if let routes = json["routes"] as AnyObject? as? [AnyObject] {
                    if let route = routes.first as? [String : AnyObject] {
                        if let polyline = route["overview_polyline"] as AnyObject? as? [String : String] {
                            if let points = polyline["points"] as AnyObject? as? String {
                                encodedRoute = points
                            }
                        }
                    }
                }
            }
        } catch {

        }

        dispatch_async(dispatch_get_main_queue()) {
            completion(encodedRoute)
        }
    }.resume()

Upvotes: 1

Related Questions