Swift:调用中的额外参数 'error'
Swift: Extra argument 'error' in call
我目前正在使用 Swift 2.0 和 Xcode Beta 2 开发我的第一个 iOS 应用程序。它读取外部 JSON 并在 table 用数据查看。但是,我遇到了一个似乎无法修复的奇怪小错误:
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{
有人可以告诉我我做错了什么吗?
随着 Swift 2,NSJSONSerialization
的 signature 已更改,以符合新错误处理系统。
这是一个如何使用它的例子:
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
随着 Swift 3,NSJSONSerialization
的 name 及其方法发生了变化,根据 the Swift API Design Guidelines.
这是同一个例子:
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
在 Swift 2 中发生了变化,接受 error
参数的方法被转换为抛出该错误的方法,而不是通过 inout
参数返回错误。通过查看 Apple documentation:
HANDLING ERRORS IN SWIFT:
In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.
You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 2.1) and Error Handling in Using Swift with Cocoa and Objective-C (Swift 2.1).
最短的解决方案是使用 try?
其中 returns nil
如果发生错误:
let message = try? NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
if let dict = message as? NSDictionary {
// ... process the data
}
如果您也对该错误感兴趣,可以使用 do/catch
:
do {
let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
if let dict = message as? NSDictionary {
// ... process the data
}
} catch let error as NSError {
print("An error occurred: \(error)")
}
这已在 Swift 3.0 中更改。
do{
if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{
if JSONSerialization.isValidJSONObject(responseObj){
//Do your stuff here
}
else{
//Handle error
}
}
else{
//Do your stuff here
}
}
catch let error as NSError {
print("An error occurred: \(error)") }
我目前正在使用 Swift 2.0 和 Xcode Beta 2 开发我的第一个 iOS 应用程序。它读取外部 JSON 并在 table 用数据查看。但是,我遇到了一个似乎无法修复的奇怪小错误:
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{
有人可以告诉我我做错了什么吗?
随着 Swift 2,NSJSONSerialization
的 signature 已更改,以符合新错误处理系统。
这是一个如何使用它的例子:
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
随着 Swift 3,NSJSONSerialization
的 name 及其方法发生了变化,根据 the Swift API Design Guidelines.
这是同一个例子:
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
在 Swift 2 中发生了变化,接受 error
参数的方法被转换为抛出该错误的方法,而不是通过 inout
参数返回错误。通过查看 Apple documentation:
HANDLING ERRORS IN SWIFT: In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.
You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 2.1) and Error Handling in Using Swift with Cocoa and Objective-C (Swift 2.1).
最短的解决方案是使用 try?
其中 returns nil
如果发生错误:
let message = try? NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
if let dict = message as? NSDictionary {
// ... process the data
}
如果您也对该错误感兴趣,可以使用 do/catch
:
do {
let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
if let dict = message as? NSDictionary {
// ... process the data
}
} catch let error as NSError {
print("An error occurred: \(error)")
}
这已在 Swift 3.0 中更改。
do{
if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{
if JSONSerialization.isValidJSONObject(responseObj){
//Do your stuff here
}
else{
//Handle error
}
}
else{
//Do your stuff here
}
}
catch let error as NSError {
print("An error occurred: \(error)") }