UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Unwrapping Date?

Forums > SwiftUI

I am using the weatherkit and am trying to unwrap sunset:

I constantly get a nil value.

var civilDusk: String {
        let civilDusk =
        sunEvents?.civilDusk?.formatted(date: .abbreviated, time: .shortened)
        let convert = civilDusk?.description
        return convert ?? "Civil Dusk not found"
    }

2      

let civilDusk = sunEvents?.civilDusk?.formatted(date: .abbreviated, time: .shortened)

There are two Optionals chained in there. Are you sure you have legit values for sunEvents and civilDusk?

2      

Code below, I am entering lat and long values... I dont have a locale value perhaps? Do you think it has anything to do with that?

@MainActor class WeatherKitManager: ObservableObject {

    @Published var weather: Weather?
    @Published var sunEvents: SunEvents?

    func getWeather(latitude: Double, longitude: Double) async {
            do {
                weather = try await Task.detached(priority: .userInitiated) {
                    return try await WeatherService.shared.weather(for: .init(latitude: latitude, longitude: longitude))
                }.value

            } catch {
                fatalError("\(error)")
            }
        }

    //this function below from hacking with swift allows one to bypass adding the task option in the content view and just run the function below directly, not sure what to put in the arguments for the function.
    func doGetWeather() {
        Task {
            await getWeather(latitude:Double() ,longitude: Double())
        }
    }

    var symbol: String {
        weather?.currentWeather.symbolName ?? "xmark"
    }

     var civilDawn: String {
        let civilDawn =
        sunEvents?.civilDawn?.description
        let convert = civilDawn?.description
        return convert ?? "Loading Weather Data"
    }

    var civilDusk: String {
        var civilDusk =
        sunEvents?.civilDusk?.formatted(date: .abbreviated, time: .shortened).description
        return civilDusk ?? "Civil Dusk not found"
    }

}

2      

Where do you set a value for sunEvents? Your getWeather(latitude:longitude:) function sets weather to whatever is returned by the API call, but nothing in the code you posted sets sunEvents, so it is nil and the code to calculate civilDusk (and civilDawn) will fail.

2      

Hmmm...

So I didnt need to actually use sun events. I need to call on the daily forecast... see below! Thanks again @roosterboy

 var civilDawn: String {
        let civilDawn =
        weather?.dailyForecast[0].sun.civilDawn?.formatted(date: .abbreviated, time: .shortened)
        return civilDawn ?? "Loading Weather Data"
    }

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.