taskIdentifier收到错误[Swift]

时间:2015-09-07 14:39:14

标签: ios google-maps

这是我的代码如下。

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'

我不确定代码的哪一部分出错了。我试图谷歌并使用示例代码,但错误继续。不确定是不是因为Xcode 7 beta 6

1 个答案:

答案 0 :(得分:1)

任务必须由会话创建。

var placesTask = NSURLSessionDataTask()无效