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

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{

谁能告诉我在这里做错了什么吗?


开心每一天1111
浏览 627回答 3
3回答

梵蒂冈之花

带着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)}

蛊毒传说

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

ABOUTYOU

这在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)") }
打开App,查看更多内容
随时随地看视频慕课网APP