OStack程序员社区-中国程序员成长平台

标题: ios - taskIdentifier 收到错误 [Swift] [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:29
标题: ios - taskIdentifier 收到错误 [Swift]

这是我下面的代码。

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]: unrecognized selector sent to instance 0x7f8eba441e60

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLSessionDataTask taskIdentifier]: unrecognized selector sent to instance 0x7f8eba441e60'

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



Best Answer-推荐答案


任务必须由 session 创建。

var placesTask = NSURLSessionDataTask() 无效

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4