Swift:调用中的额外参数“错误" [英] Swift: Extra argument 'error' in call
本文介绍了Swift:调用中的额外参数“错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用 Swift 2.0 和 Xcode Beta 2 开发我的第一个 iOS 应用程序.它读取外部 JSON 并在包含数据的表视图中生成一个列表.但是,我遇到了一个似乎无法修复的奇怪小错误:
I'm currently developing my first iOS app using Swift 2.0 and Xcode Beta 2. It reads an external JSON and generates a list in a table view with the data. However, I'm getting a strange little error that I can't seem to fix:
Extra argument 'error' in call
这是我的代码片段:
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
print("Task completed")
if(error != nil){
print(error!.localizedDescription)
}
var err: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{
if(err != nil){
print("JSON Error (err!.localizedDescription)")
}
if let results: NSArray = jsonResult["results"] as? NSArray{
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.appsTableView!.reloadData()
})
}
}
})
在这一行抛出错误:
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{
有人可以告诉我我在这里做错了什么吗?
Can someone please tell me what I'm doing wrong here?
推荐答案
在 Swift 2 中,NSJSONSerialization
的 signature 已更改为符合新的错误处理系统.
With Swift 2, the signature for NSJSONSerialization
has changed, to conform to the new error handling system.
以下是如何使用它的示例:
Here's an example of how to use it:
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
根据 Swift API 设计指南.
这是相同的例子:
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
这篇关于Swift:调用中的额外参数“错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文