你读到这篇文章之前,请理解,我有点斯威夫特与一个初学者,我努力学习。我已经通过一些网站在得到答案的希望浏览,但我要么无法找到或做错误的。我一直在关注这个教程,但这是老不更新>>Tutorial I followed < <URLSession&JSONSerialization斯威夫特3
我也试图修改一些快速3 - 虽然我可能没有这样做。
我该如何正确地做URLSession?我得到这个错误:
invalid conversion from throwing function of type'(_, _, _) throws -> Void' to non-throwing function type
下面这一行:
let task : URLSessionDataTask = session.dataTask(with: request, completionHandler: {data, response, error -> Void in
和变量 “jsonDict” - 我得到的错误,我有事先
extra arg 'error' in call.
谢谢
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()
}
它应该是'var jsonDict = try JSONSerialization.JSONObject(with:data,options:JSONSerialization.ReadingOptions.mutableContainers)as NSDictionary',in swift 3 error argument removed – Vinodh
@Vinodh - 谢谢,我不认为这会很简单。 – zya
邮政它作为一个答案它会为别人 – Vinodh