繁体   English   中英

通话中出现额外的参数“错误”-无法构建我的Xcode项目

[英]Extra argument 'error' in call - Unable to build my Xcode project

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

// 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)
                    completion(jsonDictionary)
                default:
                    print("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
                }
            } else {
                print("Error: Not a valid HTTP response")
            }
        }

        dataTask.resume()
    }
}

在“使用数据创建JSON对象”步骤中,我不断收到“调用中的额外参数'error'”。 怎么了? 我找不到文档来进一步帮助我。

您可以通过这种方式做到这一点。

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

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

添加此行:

var err: NSError?

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

终于想通了! 谢谢大家的投入!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM