-- 完成 URLRequest 的函数调用 - JSON
-- function-call with completion URLRequest - JSON
我确实阅读了很多有关带有完成处理程序的函数的信息,但现在我遇到了如何以正确的方式调用此函数 (downloadJSON) 的问题。我必须在函数中提供哪些参数并在我自己的 class 中处理结果数据 (json),在函数被调用的地方。
这是 David Tran 的代码。嗨制作了精彩的教程,但是在代码中没有调用这个函数。
let request: URLRequest
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONHandler = (JSON?, HTTPURLResponse?, Error?) -> Void
func downloadJSON(completion: @escaping JSONHandler)
{
let dataTask = session.dataTask(with: self.request) { (data, response, error) in
// OFF THE MAIN THREAD
// Error: missing http response
guard let httpResponse = response as? HTTPURLResponse else {
let userInfo = [NSLocalizedDescriptionKey : NSLocalizedString("Missing HTTP Response", comment: "")]
let error = NSError(domain: DANetworkingErrorDomain, code: MissingHTTPResponseError, userInfo: userInfo)
completion(nil, nil, error as Error)
return
}
if data == nil {
if let error = error {
completion(nil, httpResponse, error)
}
} else {
switch httpResponse.statusCode {
case 200:
// OK parse JSON into Foundation objects (array, dictionary..)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any]
completion(json, httpResponse, nil)
} catch let error as NSError {
completion(nil, httpResponse, error)
}
default:
print("Received HTTP response code: \(httpResponse.statusCode) - was not handled in NetworkProcessing.swift")
}
}
}
dataTask.resume()
}
让Xcode帮助你。输入 downlo
并按 return。 Xcode 完成功能
再按return得到参数
例如,您必须用参数名称替换占位符
downloadJSON { (json, response, error) in
if let error = error {
print(error)
} else if let json = json {
print(json)
}
}
注:
您的代码中存在致命类型不匹配错误:JSONSerialization
行的结果是 [String:Any]
但完成处理程序的第一个参数是 JSON
我确实阅读了很多有关带有完成处理程序的函数的信息,但现在我遇到了如何以正确的方式调用此函数 (downloadJSON) 的问题。我必须在函数中提供哪些参数并在我自己的 class 中处理结果数据 (json),在函数被调用的地方。
这是 David Tran 的代码。嗨制作了精彩的教程,但是在代码中没有调用这个函数。
let request: URLRequest
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONHandler = (JSON?, HTTPURLResponse?, Error?) -> Void
func downloadJSON(completion: @escaping JSONHandler)
{
let dataTask = session.dataTask(with: self.request) { (data, response, error) in
// OFF THE MAIN THREAD
// Error: missing http response
guard let httpResponse = response as? HTTPURLResponse else {
let userInfo = [NSLocalizedDescriptionKey : NSLocalizedString("Missing HTTP Response", comment: "")]
let error = NSError(domain: DANetworkingErrorDomain, code: MissingHTTPResponseError, userInfo: userInfo)
completion(nil, nil, error as Error)
return
}
if data == nil {
if let error = error {
completion(nil, httpResponse, error)
}
} else {
switch httpResponse.statusCode {
case 200:
// OK parse JSON into Foundation objects (array, dictionary..)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any]
completion(json, httpResponse, nil)
} catch let error as NSError {
completion(nil, httpResponse, error)
}
default:
print("Received HTTP response code: \(httpResponse.statusCode) - was not handled in NetworkProcessing.swift")
}
}
}
dataTask.resume()
}
让Xcode帮助你。输入 downlo
并按 return。 Xcode 完成功能
再按return得到参数
例如,您必须用参数名称替换占位符
downloadJSON { (json, response, error) in
if let error = error {
print(error)
} else if let json = json {
print(json)
}
}
注:
您的代码中存在致命类型不匹配错误:JSONSerialization
行的结果是 [String:Any]
但完成处理程序的第一个参数是 JSON