ios - taskIdentifier 收到错误 [Swift]
<p><p>这是我下面的代码。 </p>
<pre><code>var placesTask = NSURLSessionDataTask()
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:, completion: (() -> 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 = ()
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()
}
</code></pre>
<p>我得到的错误:</p>
<blockquote>
<p>-: unrecognized selector sent to instance 0x7f8eba441e60</p>
<p>*** Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '-[NSURLSessionDataTask
taskIdentifier]: unrecognized selector sent to instance
0x7f8eba441e60'</p>
</blockquote>
<p>我不确定代码的哪一部分出错了。我试图谷歌并使用示例代码,但错误不断出现。不确定是不是因为 Xcode 7 beta 6</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>任务必须由 session 创建。 </p>
<p>var placesTask = NSURLSessionDataTask() 无效</p></p>
<p style="font-size: 20px;">关于ios - taskIdentifier 收到错误 ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/32441197/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/32441197/
</a>
</p>
页:
[1]