Swift2 iOS 9(Swift 2.0):无法调用';带有URL'的数据任务;具有类型为';(NSURL),(uu,u,u,u,u)抛出->;无效)和"x27 ;;
我的应用程序在iOS 8上运行得非常好,但昨天我升级到iOS 9和Xcode 7,然后我的应用程序崩溃了。错误消息为无法使用类型为“(NSURL,(,)throws->Void)”的参数列表调用“dataTaskwithURL”。我在谷歌上搜索了一下,发现了一个类似的问题,但解决方案并没有真正起作用(解决方案是在代码周围添加do/catch块)。有人能帮我解决我的问题吗?谢谢你 这是我的密码Swift2 iOS 9(Swift 2.0):无法调用';带有URL'的数据任务;具有类型为';(NSURL),(uu,u,u,u,u)抛出->;无效)和"x27 ;;,swift2,ios9,Swift2,Ios9,我的应用程序在iOS 8上运行得非常好,但昨天我升级到iOS 9和Xcode 7,然后我的应用程序崩溃了。错误消息为无法使用类型为“(NSURL,(,)throws->Void)”的参数列表调用“dataTaskwithURL”。我在谷歌上搜索了一下,发现了一个类似的问题,但解决方案并没有真正起作用(解决方案是在代码周围添加do/catch块)。有人能帮我解决我的问题吗?谢谢你 这是我的密码 import UIKit import Foundation import CoreLocation
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{
}
}
}
抱歉这是我第一次发布代码样式有点混乱抱歉缩进混乱:)
再次感谢 dataTaskWithURL:completionHandler:未引发错误 将do和catch放入带有URL的数据任务中 例如:
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()
有一天,Swift编译器会准确地告诉我们哪个参数的类型是错误的。。。但今天不行!说真的,有时候即使在查看文档中的方法声明后,它也不明显。特别是在从Swift 1.2过渡到2.0的过程中。。。