调用中有额外的参数“错误". Swift 2.0中的编译错误 [英] Extra argument 'error' in call. Compilation error in Swift 2.0

查看:101
本文介绍了调用中有额外的参数“错误". Swift 2.0中的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过以下示例应用程序了解Swift 2.0和OAuth集成: https://github. com/soundcloud/iOSOAuthDemo

I am currently trying to learn about Swift 2.0 and OAuth integration via this sample application: https://github.com/soundcloud/iOSOAuthDemo

下面的片段导致我出现问题,并导致应用程序编译失败.

The following snippet below is causing me problems and causing the application to fail in its compilation.

private func requestMe(token: String) {
    let url = NSURL(string: "https://api.soundcloud.com/me.json?oauth_token=\(token)")!
    let request = NSURLRequest(URL: url)
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
                               delegate: nil, delegateQueue: NSOperationQueue.mainQueue())

    let dataTask = session.dataTaskWithURL(url) { (data, response, error) -> Void in
        if let jsonOutput = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String:AnyObject] {
            self.displayMe(jsonOutput)
        }
    }
    dataTask.resume()
}

但是,在编译时,我的错误处理似乎在此版本的Swift(2.0)中已更改,并导致以下错误:

However when compiling my error handling looks as though it has changed in this version of Swift (2.0) and is causing the following error:

在编译中调用额外的参数'error'.

Extra argument 'error' in call with the compilation.

我已经查看了有关此问题的以下堆栈文章: Swift:额外参数'error通话中

I have reviewed the following stack posting on this issue: Swift: Extra argument 'error' in call

并调整了我的代码以尝试更正错误处理,例如:

and adjusted my code to try and correct the error handling as such:

let dataTask = session.dataTaskWithURL(url) { (data, response, error) -> Void in
        if let jsonOutput = NSJSONSerialization.JSONObjectWithData(data, options: nil) as? [String:AnyObject] {
            self.displayMe(jsonOutput)
        }
    }
    catch let error as NSError {
        print(error);}
    dataTask.resume()
}

我也尝试过更改:

(data, options: nil, error: nil)

(data:NSData?, error:NSError?)

但是这些方法都不能解决问题.有人可以指导我有关此错误处理可能是一个愚蠢的错误吗.

However neither of these resolving the issue. Can someone guide me as to what is probably a silly mistake I am making with this error handling.

谢谢!,

推荐答案

您的代码存在几个问题:您添加了catch,但忘记了dotry.另外,对于NSJSONSerialization,您不能再将nil作为选项参数传递,并且必须安全地展开data可选参数.

There were several problems with your code: you added catch but forgot do and try. Also you can't pass nil as an option parameter anymore for NSJSONSerialization, and you have to safely unwrap the data optional.

这是固定版本:

let dataTask = session.dataTaskWithURL(url) { (data, response, error) -> Void in
    do {
        if let myData = data, let jsonOutput = try NSJSONSerialization.JSONObjectWithData(myData, options: []) as? [String:AnyObject] {
            self.displayMe(jsonOutput)
        }
    } catch let error as NSError {
        print(error)
    }
}
dataTask.resume()

这篇关于调用中有额外的参数“错误". Swift 2.0中的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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