![](/img/trans.png)
[英]canEvaluatePolicy Extra argument 'error' in call Swift Xcode 7
[英]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.