xcode 调用中的额外参数“错误”。Swift 2.0 中的编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32809294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:57:00  来源:igfitidea点击:

Extra argument 'error' in call. Compilation error in Swift 2.0

iosxcodeswift2

提问by user4657

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

我目前正在尝试通过此示例应用程序了解 Swift 2.0 和 OAuth 集成: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()
}

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:

但是,在编译我的错误处理时,它似乎在此版本的 Swift (2.0) 中发生了变化,并导致以下错误:

Extra argument 'error' in call with the compilation.

编译时调用的额外参数“错误”。

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

我已经查看了有关此问题的以下堆栈发布: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()
}

I have also tried changing:

我也试过改变:

(data, options: nil, error: nil)

to

(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.

然而,这些都没有解决问题。有人可以指导我了解我在此错误处理中犯的一个愚蠢的错误。

Thanks in advance!,

提前致谢!,

回答by Eric Aya

There were several problems with your code: you added catchbut forgot doand try. Also you can't pass nilas an option parameter anymore for NSJSONSerialization, and you have to safely unwrap the dataoptional.

您的代码有几个问题:您添加catch但忘记了dotry。此外,您不能nil再将 NSJSONSerialization 作为选项参数传递,并且您必须安全地解开data可选参数。

Here's a fixed version:

这是一个固定版本:

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()