Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 额外参数';错误';随时待命。Swift 2.0中的编译错误_Ios_Xcode_Swift2 - Fatal编程技术网

Ios 额外参数';错误';随时待命。Swift 2.0中的编译错误

Ios 额外参数';错误';随时待命。Swift 2.0中的编译错误,ios,xcode,swift2,Ios,Xcode,Swift2,我目前正试图通过以下示例应用程序了解Swift 2.0和OAuth集成: 下面的代码段导致了我的问题,并导致应用程序编译失败 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 =

我目前正试图通过以下示例应用程序了解Swift 2.0和OAuth集成:

下面的代码段导致了我的问题,并导致应用程序编译失败

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)中发生了更改,并导致以下错误:

编译调用中的额外参数“error”

我已审阅了有关此问题的以下帖子:

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

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)

然而,这两种方法都不能解决问题。有没有人能告诉我,在这个错误处理过程中,我可能犯了一个愚蠢的错误


提前谢谢

您的代码有几个问题:您添加了
catch
,但忘记了
do
try
。此外,对于NSJSONSerialization,您不能再将
nil
作为选项参数传递,您必须安全地展开
数据
可选

这里有一个固定版本:

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

谢谢你,埃里克·D!
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()