Wenn Sie etwas tun wollen nach getWeather
getan wird, einen Abschluss-Handler hinzuzufügen. Persönlich würde ich dies vereinfachen, indem eine Struktur zu schaffen, die erfassten Informationen weitergeben zurück:
struct WeatherReport {
let latitude: Double
let longitude: Double
let temperature: Double
let name: String
let desc: String
}
func getWeather(city: String, completion: @escaping (WeatherReport?, Error?) -> Void) {
...
let task = session.dataTask(with: url!) { data, _, error in
...
guard successful else {
completion(nil, error)
return
}
let weatherReport = WeatherReport(...)
completion(weatherReport, nil)
}
task.resume()
}
Dann
override func viewDidLoad() {
super.viewDidLoad()
getWeather(city: "Minsk") { weatherReport, error in
guard let weatherReport = weatherReport, error == nil else {
print(error?.localizedDescription ?? "Unknown error")
return
}
DispatchQueue.main.async {
// do something with `weatherReport` here
}
}
}
Hinweis, dataTask
bereits (a) asynchron ausgeführt wird; und (b) läuft seine Fertigstellung Handler auf einem Hintergrund-Thread, so viewDidLoad
muss ausdrücklich etwas versenden es tut mit Modellobjekten und/oder der Benutzeroberfläche an der Hauptwarteschlange in der getWeather
Abschluss handler wie oben gezeigt.
By the way, wenn Sie Swift mit 4 sind, würde ich vorschlagen, auszuschließen, dass Dritte JSON-Parsing-Bibliothek und verwendet JSONDecoder
und eine Modellstruktur, die Decodable
, zB:
struct Coordinate: Decodable {
let latitude: Double
let longitude: Double
enum CodingKeys: String, CodingKey {
case latitude = "lat"
case longitude = "lon"
}
}
struct WeatherReportDetails: Decodable {
let temperature: Double
let pressure: Double
let humidity: Double
let temperatureMin: Double
let temperatureMax: Double
enum CodingKeys: String, CodingKey {
case pressure, humidity
case temperature = "temp"
case temperatureMin = "temp_min"
case temperatureMax = "temp_max"
}
}
struct WeatherReport: Decodable {
let coordinate: Coordinate
let details: WeatherReportDetails
let name: String
enum CodingKeys: String, CodingKey {
case name
case coordinate = "coord"
case details = "main"
}
}
und dann
func getWeather(city: String, completion: @escaping (WeatherReport?, Error?) -> Void) {
let cityEscaped = city.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)!
let urlString = "http://samples.openweathermap.org/data/2.5/weather?q=" + cityEscaped + "&appid=..." // appid removed for privacy’s sake
let url = URL(string: urlString)!
let task = URLSession.shared.dataTask(with: url) { data, _, error in
guard let data = data, error == nil else {
completion(nil, error)
return
}
do {
let weatherReport = try JSONDecoder().decode(WeatherReport.self, from: data)
completion(weatherReport, nil)
} catch {
completion(nil, error)
}
}
task.resume()
}
übersetzen Ihre Frage ... –
möchten Sie erste Druckergebnis dann herunterladen? –
zu „Hallo“ gebracht, nachdem „lat lon“ bedeuten? –