最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - WeatherAlert has no member 'onsetTime', 'expirationDate', 'issuedDate',

programmeradmin4浏览0评论

I am using Xcode Version 16.2 (16C5032a) and WeatherKit API (not REST API). When debugging the return from a WeatherKit request, Xcode is displaying that the returned WeatherKit.WeatherAlert structs have several new variables: 'onsetTime', 'expirationDate', 'issuedDate', 'endDate' IN ADDITION TO the metadata variable. These are direct members of the WeatherKit.WeatherAlert struct, not the metadata struct.

However Xcode issues a compiler error "WeatherAlert has no member 'onsetTime',..." when I try to code access to them, and the latest documentation does not reference them.

Is there any way to access these vars without compiler warnings (and a crash!) using Swift?

Is there any updated documentation for them? It seems Apple have introduced these options without any way to access them. Interestingly they mirror variables available via REST API request since the beginning.

Thanks

Here is the code:

import Foundation
import WeatherKit
import CoreLocation

@available(iOS 16.0, *)
func queryWeather() {
    let location = CLLocation(latitude: 37.69300079345703, longitude: -97.33899688720703)
    Task.detached(priority: .userInitiated) {
        if let (alerts) = try? await WeatherService.shared.weather(for: location, including: .alerts),
           let alert = alerts.first
        {
            print(alert)
            print(alert.onsetTime) // compiler error: "Value of type 'WeatherAlert' has no member 'onsetTime'"

        }
    }
}

The line

print(alert.onsetTime)

gives the compiler error "WeatherAlert has no member 'onsetTime'" If removed then the code runs and the line

print(alert)

gives the following output, in which the variable 'onsetTime' is clearly present:

Optional(WeatherKit.WeatherAlert(detailsURL: .html?ids=3a1f2813-6f68-5186-b4c2-300811da2840&lang=en-GB&timezone=America/Chicago, source: "National Weather Service", date: 2025-02-19 16:03:10 +0000, expirationDate: 2025-02-20 00:00:00 +0000, issuedDate: 2025-02-19 08:46:00 +0000, onsetTime: Optional(2025-02-19 08:46:00 +0000), endDate: Optional(2025-02-20 18:00:00 +0000), summary: "Extreme Cold Warning", description: "Extreme Cold Warning", details: nil, region: nil, severity: severe, importance: High, metadata: WeatherKit.WeatherMetadata(date: 2025-02-19 16:03:10 +0000, expirationDate: 2025-02-19 16:08:10 +0000, latitude: 37.69300079345703, longitude: -97.33899688720703, attribution: Optional(WeatherKit.WeatherAttribution(storage: WeatherKit.WeatherAttribution.(unknown context at $1bee7afa8).Storage))), id: 3A1F2813-6F68-5186-B4C2-300811DA2840))

The debugger shows the following in the Variable View, with the variables 'onsetTime', 'expirationDate', 'issuedDate', 'endDate' present (I have removed the values for clarity).

alerts  [WeatherKit.WeatherAlert]?  1 value
[0] WeatherKit.WeatherAlert 
    detailsURL          Foundation.URL  
    source              String  
    date                Foundation.Date
    expirationDate      Foundation.Date     
    issuedDate          Foundation.Date     
    onsetTime           Foundation.Date?    
    endDate             Foundation.Date?    
    summary             String
    description         String
    details             Foundation.URL? 
    region              String? 
    severity            WeatherKit.WeatherSeverity  
    importance          WeatherKit.WeatherImportance    
    metadata            WeatherKit.WeatherMetadata  
        date                Foundation.Date     
        expirationDate      Foundation.Date     
        latitude            Double  
        longitude           Double  
        attribution         WeatherKit.WeatherAttribution?
    id                  Foundation.UUID 

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论