Very new to SwiftUI. I'm working on a very simple weather app using Apple's WeatherKit. Have no problem getting the temperature and other parameters but I'd like to change the systemImage icon (sun, cloud, moon, etc...) to a corresponding emoji. Tried to create an enum with no luck:
import Foundation
import WeatherKit
import SwiftUI
@MainActor class WeatherKitManager: ObservableObject {
@Published var weather: Weather?
func getWeather() async {
do {
weather = try await Task.detached(priority: .userInitiated) {
return try await WeatherService.shared.weather(for: .init(latitude: 42.28851, longitude: 13.55401)) // Coordinates for SAN PIO just as example coordinates
}.value
} catch {
fatalError("\(error)")
}
}
enum WeatherEmoji: String, Codable {
case blowingDust = "blowingDust"
case clear = "clear"
case cloudy = "cloudy"
case foggy = "foggy"
case haze = "haze"
case mostlyClear = "mostlyClear"
case mostlyCloudy = "mostlyCloudy"
case partlyCloudy = "partlyCloudy"
case smokey = "smokey"
case breezy = "breezy"
case windy = "windy"
case drizzle = "drizzle"
case heavyRain = "heavyRain"
case isolatedThunderstorms = "isolatedThunderstorms"
case rain = "rain"
case sunShowers = "sunShowers"
case scatteredThunderstorms = "scatteredThunderstorms"
case strongStorms = "strongStorms"
case thunderstorms = "thunderstorms"
case frigid = "frigid"
case hail = "hail"
case hot = "hot"
case flurries = "flurries"
case sleet = "sleet"
case snow = "snow"
case sunFlurries = "sunFlurries"
case wintryMix = "wintryMix"
case blizzard = "blizzard"
case blowingSnow = "blowingSnow"
case freezingDrizzle = "freezingDrizzle"
case freezingRain = "freezingRain"
case heavySnow = "heavySnow"
case hurricane = "hurricane"
case tropicalStorm = "tropicalStorm"
var emojiweather: LocalizedStringKey {
switch self {
case .clear:
return "βοΈ"
case .blowingDust:
return "π¨"
case .cloudy:
return "βοΈ"
case .foggy:
return "π«οΈ"
case .haze:
return "πΆβπ«οΈ"
case .mostlyClear:
return "π€οΈ"
case .mostlyCloudy:
return "π₯οΈ"
case .partlyCloudy:
return "β
οΈ"
case .smokey:
return "πΆβπ«οΈ"
case .breezy:
return "π¨"
case .windy:
return "π"
case .drizzle:
return "βοΈ"
case .heavyRain:
return "π§οΈ"
case .isolatedThunderstorms:
return "β‘οΈ"
case .rain:
return "π§οΈ"
case .sunShowers:
return "π€οΈ"
case .scatteredThunderstorms:
return "β‘οΈ"
case .strongStorms:
return "βοΈ"
case .thunderstorms:
return "βοΈ"
case .frigid:
return "π§£"
case .hail:
return "βοΈ"
case .hot:
return "π₯"
case .flurries:
return "π¨"
case .sleet:
return "π¨οΈ"
case .snow:
return "βοΈ"
case .sunFlurries:
return "π€οΈ"
case .wintryMix:
return "π§£"
case .blizzard:
return "π¨οΈ"
case .blowingSnow:
return "π¨οΈ"
case .freezingDrizzle:
return "π₯Ά"
case .freezingRain:
return "π§οΈ"
case .heavySnow:
return "π¨οΈ"
case .hurricane:
return "πͺοΈ"
case .tropicalStorm:
return "πͺοΈ"
}
}
}
var lookslikemoji: String {
let myemoji = "\(WeatherEmoji.self)"
return "\(myemoji)"
}
var symbol: String {
weather?.currentWeather.symbolName ?? "xmark"
}
var temp: String {
let temp = weather?.currentWeather.temperature
let convert = temp?.converted(to: .celsius).formatted(.measurement(width: .narrow, usage: .asProvided, numberFormatStyle: .number.precision(.fractionLength(0))))
return convert ?? "Loading Weather Data"
}
var feelslike: String {
let feelslike = weather?.currentWeather.apparentTemperature
let convert = feelslike?.converted(to: .celsius).formatted(.measurement(width: .narrow, usage: .asProvided, numberFormatStyle: .number.precision(.fractionLength(0))))
return convert ?? "Loading Weather Data"
}
}
What am I am doing wrong? Thank you.
A better way is to convert the WeatherCondition
enum case which matches the symbol
property
Declare an extension of WeatherCondition
with a computed property emoji
extension WeatherCondition {
var emoji : String {
switch self {
}
}
}
The compiler will show a Switch must be exhaustive
error. Click on the little red circle and then on Fix
. This adds stubs for all cases.
Return the corresponding emoji for each case for example
case .blizzard: return "π¨οΈ"
case .blowingDust: return "π¨"
...
In the view show the emoji with weather.condition.emoji