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
}
关于ios - 调用中的额外参数“错误”-无法构建我的Xcode项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32369285/