2
這是我的代碼如下。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]:無法識別的選擇發送到實例0x7f8eba441e60
***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [NSURLSessionDataTask taskIdentifier]:發送到實例的無法識別的選擇器 0x7f8eba441e60'
我不確定哪部分代碼出錯了。我試圖谷歌和使用示例代碼,但錯誤不斷。不知道是否是因爲Xcode 7 beta 6
你在哪裏聲明,併成立,變量'placesTask'? – Robert
@Robert感謝您的回覆。我在頂部宣佈它。我編輯了我的帖子 –
使用Xcode 6.4運行良好,該屬性可以從Swift需要的iOs 7.0中獲得,因此不應該成爲問題。如果您點擊或cmd +單擊該屬性,會發生什麼情況? –