Swift:调用中的额外参数'错误' [英] Swift: Extra argument 'error' in call

查看:123
本文介绍了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 已更改,以符合新的错误处理系统。

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 3 NSJSONSerialization的名称 Swift API设计指南,code>及其方法已发生变化。

With Swift 3, the name of NSJSONSerialization and its methods have changed, according to 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:调用中的额外参数'错误'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆