这是我的下面的代码。

var placesTask = NSURLSessionDataTask()

func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: (([GooglePlace]) -> Void)) -> ()
  {
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(apiKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&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

    placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error 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)
      }
    }
    placesTask.resume()
  }

我得到的错误:

-[NSURLSessionDataTask taskIdentifier]:无法识别的选择器已发送到实例0x7f8eba441e60

***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSURLSessionDataTask
[taskIdentifier]:无法识别的选择器已发送到实例
0x7f8eba441e60'

我不确定代码的哪一部分出错了。我尝试使用Google并使用示例代码,但错误不断出现。不确定是否是因为Xcode 7 beta 6

最佳答案

该任务必须由 session 创建。

var placeTask = NSURLSessionDataTask()无效

关于ios - taskIdentifier出现错误[Swift],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32441197/

10-12 13:37