调用 'updateWeatherIcon(condition:)' 的结果未被使用,因此不会显示 UIImage

Result of call to 'updateWeatherIcon(condition:)' is unused and therefore will not show UIImage

我正在构建一个访问 openweathermap API 的小型天气应用程序。我正在使用 JSONDecoderAPI 解析 JSON。在大多数情况下,我能够获取 simulator 中的大部分数据。除了应该出现在屏幕上的 UIImage。图片在 image.xcassets 中。下面是结构。

import UIKit
import CoreLocation

struct WeatherData: Codable {
let coord: Coord
let weather: [Weather]
let base: String
let main: Main
let visibility: Int
let wind: Wind
let clouds: Clouds
let dt: Int
let sys: Sys
let id: Int
let name: String
let cod: Int
 }

struct Clouds: Codable {
let all: Int
}

struct Coord: Codable {
let lon, lat: Double
}

struct Main: Codable {
let temp: Double
let pressure, humidity: Int
let tempMin, tempMax: Double

//    enum CodingKeys: String, CodingKey {
 //        case temp, pressure, humidity
 //        case tempMin = "temp_min"
 / /        case tempMax = "temp_max"
 //    }
 }

struct Sys: Codable {
let type, id: Int
let message: Double
let country: String
let sunrise, sunset: Int
}

struct Weather: Codable {
let id: Int
let main, description, icon: String
}

struct Wind: Codable {
let speed: Double
let deg: Int
}

通过JSON访问的代码如下:

private func getWeatherData(parameters: [String : String]) {
    guard let lat = parameters["lat"],
        let long = parameters["long"],
        let appID = parameters["appid"] else { print("Invalid parameters"); return }
    var urlComponents = URLComponents(string: "https://api.openweathermap.org/data/2.5/weather")!
    let queryItems = [URLQueryItem(name: "lat", value: lat),
                      URLQueryItem(name: "lon", value: long),
                      URLQueryItem(name: "appid", value: appID)]
    urlComponents.queryItems = queryItems

    guard let url = urlComponents.url else { return }
    URLSession.shared.dataTask(with: url) { ( data, response, err ) in
        DispatchQueue.main.async { // never, never, never sync !!
            if let err = err {
                print("Failed to get data from url:", err)
                return
            }
            guard let data = data else { return }
            do {
                let decoder = JSONDecoder()
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                let city = try decoder.decode(WeatherData.self, from: data)
                print(city)
                //self.updateWeatherData(city)
               self.weatherData.description = city.weather[0].main
                self.weatherData.temperature = Int(city.main.temp - 273)
                self.weatherData.city = city.name
                self.weatherData.condition = city.weather[0].id
                 WeatherDataModel().updateWeatherIcon(condition: self.weatherData.condition)
                self.updateUIWeatherData()

            } catch {
                print(error)
                self.cityLabel.text = "Connection issues"
            }
        }
        }.resume()
}

在模拟器上显示数据的函数是:

func updateUIWeatherData() {
    cityLabel.text = weatherData.city
    temperatureLabel.text = String(weatherData.temperature)
    decriptionLabel.text = String(weatherData.description)
    weatherIcon.image = UIImage(named: weatherData.weatherIconName)
}

我查看了此警告的其他示例,但不太确定这对这个应用程序意味着什么。 。 任何帮助将不胜感激。

在我看来你想要这条线

self.weatherData.weatherIconName = WeatherDataModel().updateWeatherIcon(condition: self.weatherData.condition)

而不是

WeatherDataModel().updateWeatherIcon(condition: self.weatherData.condition)

警告说您正在计算 weatherIcon,但它没有分配给任何东西(您的 weatherData 变量)