79454772

Date: 2025-02-20 14:09:01
Score: 0.5
Natty:
Report link

I worked out a way to get around it using JSON encoding / decoding. I noted that a JSONEncoder and JSONDecoder DID encode/decode the undocumented struct properties that the Xcode compiler does not recognise. (Why? How?) I created a custom struct to decode back into with all the properties of the new WeatherKit.WeatherAlert structure. The new properties are now available to use on the MyWeatherAlertObject.

I chose to guard whether onsetTime and endDate were non-nil, so I can trigger a WeatherKit REST API lookup for these alerts, as that also returns all the properties.

@available(iOS 16.0, *)
struct MyWeatherAlertObject: Codable {
    var detailsURL: URL
    var source: String
    var date: Date
    var expirationDate: Date
    var issuedDate: Date
    var onsetTime: Date?
    var endDate: Date?
    var summary: String
    var description: String
    var details: URL?
    var region: String?
    var severity: WeatherKit.WeatherSeverity
    var importance: String
    var metadata: WeatherKit.WeatherMetadata
    var id: UUID
    
    init?(fromWeatherAlert alert: WeatherAlert?) {
        guard   let alert = alert,
                let data = try? JSONEncoder().encode(alert),
                let obj = try? JSONDecoder().decode(MyWeatherAlertObject.self, from: data),
                // no point using if these two are missing
                obj.onsetTime != nil && obj.endDate != nil
        else { return nil }
        self = obj
    }
}
Reasons:
  • Blacklisted phrase (0.5): Why?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: djmlewis