为了账号安全,请及时绑定邮箱和手机立即绑定

SWIFT:呼叫中额外的争论“错误”

SWIFT:呼叫中额外的争论“错误”

开心每一天1111 2019-06-26 17:31:02
SWIFT:呼叫中额外的争论“错误”目前,我正在使用SWIFT2.0和Xcode Beta 2开发我的第一个iOS应用程序,它读取外部JSON,并在表视图中生成包含数据的列表。然而,我遇到了一个奇怪的小错误,似乎无法修复: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{谁能告诉我在这里做错了什么吗?
查看完整描述

3 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

带着SWIFT 2签名NSJSONSerialization已更改,以符合新的错误处理系统。

下面是一个如何使用它的示例:

do {
    if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
        print(jsonResult)
    }} catch let error as NSError {
    print(error.localizedDescription)}

带着SWIFT 3名字,姓名NSJSONSerialization它的方法已经改变了,根据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)}


查看完整回答
反对 回复 2019-06-26
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

在SWIFT 2中,情况发生了变化,这些方法接受了error参数转换为抛出错误的方法,而不是通过inout参数。通过查看苹果文档:

处理SWIFT中的错误:在SWIFT中,此方法返回一个非可选的结果,并使用抛出关键字标记,以指示在发生故障时抛出错误。

您可以在try表达式中调用此方法,并处理do语句的CATCH子句中的任何错误,如SWIFT编程语言中的错误处理(SWIFT 2.1),以及在使用SWIFT与Cocoa和Object-C(SWIFT 2.1)时的错误处理。

最短的解决办法是使用try?返回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)")}


查看完整回答
反对 回复 2019-06-26
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

这在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)") }


查看完整回答
反对 回复 2019-06-26
  • 3 回答
  • 0 关注
  • 624 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信