在您阅读本文之前,请了解我在某种程度上是 Swift 的初学者,并且正在努力学习。我浏览了一些网站希望得到答案,但我找不到或做错了。我一直在关注这个教程,但这是旧的,没有更新>>我遵循的教程<<
我还尝试将一些修改为 swift 3 - 尽管我可能做得不对。
我如何正确地正确执行 URLSession?我收到此错误:
从类型为'(_, _, _) throws -> Void' 的抛出函数到非抛出函数类型的无效转换
对于下面的这一行:
let task : URLSessionDataTask = session.dataTask(with: request, completionHandler: {data, response, error -> Void in
对于变量“jsonDict”-我收到错误
调用中的额外参数“错误”。
提前致谢
var urlString:String = ("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN "+stringQuotes+"&format=json&env=http://datatables.org/alltables.env").addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
var url : URL = URL(string: urlString)!
var request: URLRequest = URLRequest(url:url)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task : URLSessionDataTask = session.dataTask(with: request, completionHandler: {data, response, error -> Void in
if((error) != nil) {
println(error.localizedDescription)
}
else {
var err: NSError?
var jsonDict = try JSONSerialization.JSONObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers, error: &err) as NSDictionary
if(err != nil) {
println("JSON Error \(err!.localizedDescription)")
}
else {
var quotes:NSArray = ((jsonDict.objectForKey("query") as NSDictionary).objectForKey("results") as NSDictionary).objectForKey("quote") as NSArray
DispatchQueue.main.async(execute: {
.default.post(name: Notification.Name(rawValue: kNotificationStocksUpdated), object: nil, userInfo: [kNotificationStocksUpdated:quotes])
})
}
}
})
task.resume()
}