调用中的额外参数'错误' - 无法构建我的Xcode项目

时间:2015-09-03 07:19:25

标签: ios json swift swift2 nsjsonserialization

like

// 1.检查HTTP响应是否成功获取GET请求

import Foundation

class NetworkOperation {

    lazy var config: NSURLSessionConfiguration =     NSURLSessionConfiguration.defaultSessionConfiguration()
    lazy var session: NSURLSession = NSURLSession(configuration: self.config)
    let queryURL: NSURL

    typealias JSONDictionaryCompletion = ([String: AnyObject]? -> Void)

    init(url: NSURL) {
        self.queryURL = url
    }

    func downloadJSONFromURL(completion: JSONDictionaryCompletion) {

        let request = NSURLRequest(URL: queryURL)
        let dataTask = session.dataTaskWithRequest(request) {
            (let data, let response, let error) in

// 2.使用数据创建JSON对象

            if let httpResponse = response as? NSHTTPURLResponse {
                switch httpResponse.statusCode {
                case 200:

在'使用数据创建JSON对象'步骤中,我一直在调用中收到“额外参数'错误'。怎么了?我无法找到文档来帮助我进一步了解。

3 个答案:

答案 0 :(得分:1)

你可以这样做。

do{
        var jsonDictionary  = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers)
        //completion(jsonDictionary)
    }catch{
       // report error
    }

答案 1 :(得分:0)

在第2步的顶部:创建json ....

添加以下行:

var err: NSError?

答案 2 :(得分:0)

// 1.检查HTTP响应是否成功获取GET请求

            if let httpResponse = response as? NSHTTPURLResponse {
                switch httpResponse.statusCode {
                case 200:

// 2.使用数据创建JSON对象

                    let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String: AnyObject]
                    completion(jsonDictionary)
                default:
                    println("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
                }
            } else {
                println("Error: Not a valid HTTP response")
            }
        }

        dataTask.resume()
    }
}

终于明白了!感谢大家的投入!