session.dataTask 调用错误中的 Swift5 额外参数 'completionHandler'
Swift5 Extra argument 'completionHandler' in call error with session.dataTask
我正在尝试制作一个天气应用程序
但有一些问题
错误是“调用中的额外参数 'completionHandler'”
我应该怎么做才能解决它??
enter image description here
import Foundation
struct WeatherManager {
let weatherURL = "http://api.openweathermap.org/data/2.5/weather?APPID=sdfkjskfjsfjskflskflsjklfjf&units=metric"
func fetchWeather(cityName: String) {
let urlStirng = "\(weatherURL)&q=\(cityName)"
performRequest(urlString: urlStirng)
}
func performRequest(urlString: String) {
//1. Create a URL
if let url = URL(string: urlString) {
//2. Create a URL Session
let session = URLSession(configuration: .default)
//3. Give the session a task
let task = session.dataTask(with: url, completionHandler: handle(data:response:error:))
//4. Start the task
task.resume()
}
}
func handle(data: Data?, response: URLSession?, error: Error?) {
}
}
检查以下代码:
func performRequest(urlString: String) {
//1. Create a URL
if let url = URL(string: urlString) {
//2. Create a URL Session
let session = URLSession(configuration: .default)
//3. Give the session a task
let task = session.dataTask(with: url, completionHandler: handle)
//4. Start the task
task.resume()
}
}
func handle(data: Data?, response: URLResponse?, error: Error?) {
}
对于 response
你需要传递一个 URLResponse
而不是 URLSession
到 handle
并且你不必写参数名称,因为你是将您的 handle
函数作为参数传递给 dataTask
.
我正在尝试制作一个天气应用程序 但有一些问题 错误是“调用中的额外参数 'completionHandler'” 我应该怎么做才能解决它?? enter image description here
import Foundation
struct WeatherManager {
let weatherURL = "http://api.openweathermap.org/data/2.5/weather?APPID=sdfkjskfjsfjskflskflsjklfjf&units=metric"
func fetchWeather(cityName: String) {
let urlStirng = "\(weatherURL)&q=\(cityName)"
performRequest(urlString: urlStirng)
}
func performRequest(urlString: String) {
//1. Create a URL
if let url = URL(string: urlString) {
//2. Create a URL Session
let session = URLSession(configuration: .default)
//3. Give the session a task
let task = session.dataTask(with: url, completionHandler: handle(data:response:error:))
//4. Start the task
task.resume()
}
}
func handle(data: Data?, response: URLSession?, error: Error?) {
}
}
检查以下代码:
func performRequest(urlString: String) {
//1. Create a URL
if let url = URL(string: urlString) {
//2. Create a URL Session
let session = URLSession(configuration: .default)
//3. Give the session a task
let task = session.dataTask(with: url, completionHandler: handle)
//4. Start the task
task.resume()
}
}
func handle(data: Data?, response: URLResponse?, error: Error?) {
}
对于 response
你需要传递一个 URLResponse
而不是 URLSession
到 handle
并且你不必写参数名称,因为你是将您的 handle
函数作为参数传递给 dataTask
.