iOS 9(Swift 2.0):无法使用类型为“(NSURL,(_,_,_)throws-> Void)”的参数列表调用“dataTaskwithURL”
Posted
技术标签:
【中文标题】iOS 9(Swift 2.0):无法使用类型为“(NSURL,(_,_,_)throws-> Void)”的参数列表调用“dataTaskwithURL”【英文标题】:iOS 9(Swift 2.0): cannot invoke 'dataTaskwithURL' with an argument list of type '(NSURL, (_, _,_)throws -> Void)' 【发布时间】:2015-09-18 03:17:06 【问题描述】:我的应用程序在 ios 8 上运行良好,但昨天我升级到 iOS 9 和 Xcode 7,然后我的应用程序崩溃了。错误消息是cannot invoke 'dataTaskwithURL' with an argument list of type '(NSURL, (_, ,)throws -> Void)'。我用谷歌搜索并在这里发现了一个类似的问题,但解决方案并没有真正起作用(解决方案是在代码周围添加 do/catch 块)。谁能帮我解决我的问题?谢谢!!!
这是我的代码
import UIKit
import Foundation
import CoreLocation
class GoogleDataProvider
let apiKey = "AIzaSyCQo-clIkek87N99RVh2lmFX9Mu9QPhAtA"
let serverKey = "AIzaSyBzmv7wPFcPAe1ucy5o6dqaXnda9i9MqjE"
var photoCache = [String:UIImage]()
var placesTask = NSURLSessionDataTask()
var session: NSURLSession
return NSURLSession.sharedSession()
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius:Double, types:[String],keyword:String, completion: (([GooglePlace]) -> Void)) -> ()
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(serverKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&keyword=\(keyword)&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
do
//******************Here's the line that displays error
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!)
(data, response, error) -> Void 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)
catch
placesTask.resume()
func fetchDirectionsFrom(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D, completion: ((String?) -> Void)) -> ()
let urlString = "https://maps.googleapis.com/maps/api/directions/json?key=\(serverKey)&origin=\(from.latitude),\(from.longitude)&destination=\(to.latitude),\(to.longitude)&mode=walking"
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
do
//******************************Here too ****************************
session.dataTaskWithURL(NSURL(string: urlString)!)
(data, response, error) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
var encodedRoute: String?
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject]
if let routes = json["routes"] as AnyObject? as? [AnyObject]
if let route = routes.first as? [String : AnyObject]
if let polyline = route["overview_polyline"] as AnyObject? as? [String : String]
if let points = polyline["points"] as AnyObject? as? String
encodedRoute = points
dispatch_async(dispatch_get_main_queue())
completion(encodedRoute)
.resume()
catch
对不起,这是我第一次发布代码风格有点混乱对不起缩进混乱:)
再次感谢!!!
【问题讨论】:
有一天,Swift 编译器会准确地告诉我们哪个参数的类型错误...... 但不是今天! i> 说真的,有时即使在查看文档中的方法声明后也并不明显。尤其是在从 Swift 1.2 到 2.0 的过渡期间... 【参考方案1】:dataTaskWithURL:completionHandler: 不抛出错误。
将 do 和 catch 放入 dataTaskWithURL 方法中。
例如:
session.dataTaskWithURL(NSURL(string: urlString)!)
(data, response, error) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
var encodedRoute: String?
do
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String:AnyObject]
if let routes = json["routes"] as AnyObject? as? [AnyObject]
if let route = routes.first as? [String : AnyObject]
if let polyline = route["overview_polyline"] as AnyObject? as? [String : String]
if let points = polyline["points"] as AnyObject? as? String
encodedRoute = points
catch
dispatch_async(dispatch_get_main_queue())
completion(encodedRoute)
.resume()
【讨论】:
以上是关于iOS 9(Swift 2.0):无法使用类型为“(NSURL,(_,_,_)throws-> Void)”的参数列表调用“dataTaskwithURL”的主要内容,如果未能解决你的问题,请参考以下文章
MKAnnotations 未显示在地图上 iOS 9 / Swift 2.0
无效的 Swift 支持 - 适用于 iOS 9 的 Swift 2.0 项目,(Xcode build 7A220)
从 iOS 9 / Swift 2.0 发送 HTTP POST 请求时,PHP 服务器没有收到正确的数据
Swift 2.0:使用 Set 时无法将值 MyApp.MyCustomClass 转换为 MyAppTests.MyCustomClass
无法使用白色按钮项色调使 UIToolBar 变为黑色(ios 9,Swift)
Swift 2.0:无法将类型“__NSArrayM”(0x10a9348d8)的值转换为“NSDictionary”(0x10a934d60)