iOS 9(Swift 2.0):无法调用" dataTaskwithURL'使用类型'(NSURL,(_,_,_)throws - > Void)的参数列表'

时间:2015-09-18 03:17:06

标签: swift2 ios9

我的应用程序与iOS 8完美配合,但昨天我升级到iOS 9和Xcode 7然后我的应用程序崩溃了。错误消息无法调用" dataTaskwithURL'使用类型'(NSURL,(_,)throws - > Void)' 的参数列表。我用Google搜索并在此处找到了类似的问题,但解决方案并没有真正起作用(解决方案是在代码周围添加do / catch块)。任何人都可以帮我解决我的问题吗?谢谢!!!

这是我的代码

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{

    }
  }
}  

很抱歉这是我第一次发帖代码风格有点令人困惑抱歉压痕:)

再次感谢!!!

1 个答案:

答案 0 :(得分:1)

dataTaskWithURL:completionHandler:不会抛出错误。

dataTaskWithURL 方法中添加执行捕获

例如:

    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()