如何使用WeatherKit获取每小时温度?

问题描述 投票:0回答:1

我正在尝试从 WeatherKit 获取每小时的气温预报;但是,我不断收到错误“无法将“预测”类型的值分配给“[HourWeather]”类型。我在 updateHourlyWeather 函数中收到错误。获取每小时天气的目标是能够使用每小时天气制作环境温度的折线图。

import SwiftUI
import CoreLocation
import WeatherKit
import Foundation

@MainActor
class WeatherData: ObservableObject {
static let shared = WeatherData()
private let service = WeatherService.shared
@Published var currentWeather: CurrentWeather?
@Published var hourlyForecast: [HourWeather] = []

func updateCurrentWeather(userLocation: CLLocation) {
    Task.detached(priority: .userInitiated) {
        do {
            let forcast = try await self.service.weather(
                for: userLocation,
                including: .current)
            DispatchQueue.main.async {
                self.currentWeather = forcast
            }
        } catch {
            print(error.localizedDescription)
        }
    }
}

func updateHourlyWeather(userLocation: CLLocation) {
    Task.detached(priority: .userInitiated) {
        do {
            let forcast = try await self.service.weather(
                for: userLocation,
                including: .hourly)
            DispatchQueue.main.async {
                self.hourlyForecast = forcast
            }
        } catch {
            print(error.localizedDescription)
        }
    }
}
}

struct LocalizedWeatherView: View {
@ObservedObject var weatherDataHelper = WeatherData.shared
@ObservedObject var userLocationHelper = WeatherLocationManager.shared
var body: some View {
    Form {
        if let currentWeather = weatherDataHelper.currentWeather {
            Section {
                Label(currentWeather.temperature.formatted(), systemImage: "thermometer")
                Label("\(Int(currentWeather.humidity * 100))%", systemImage: "humidity.fill")
                Label(currentWeather.isDaylight ? "Day time" : "Night time", systemImage: currentWeather.isDaylight ? "sun.max.fill" : "moon.stars.fill")
            } header: {
                HStack {
                    Spacer()
                    Image(systemName: currentWeather.symbolName)
                        .font(.system(size: 60))
                    Spacer()
                }
            }
        }
        Section {
            if userLocationHelper.userLocation == nil {
                Button("Load user current location") {
                    loadUserCurrentLocation()
                }
            }
            Button("Fetch current weather") {
                loadCurrentWeatherData()
            }
        }
    }
}

func loadUserCurrentLocation() {
    userLocationHelper.requestPermission()
    userLocationHelper.locationManager.requestLocation()
}

func loadCurrentWeatherData() {
    guard let userLocation = WeatherLocationManager.shared.userLocation else {
        return
    }
    Task.detached { @MainActor in
        weatherDataHelper.updateCurrentWeather(userLocation: userLocation)
    }
}
}
ios swift weather-api weatherkit
1个回答
0
投票

你应该使用

self.hourlyForecast = forcast.forecast

https://developer.apple.com/documentation/weatherkit/forecast

© www.soinside.com 2019 - 2024. All rights reserved.