Swift中如何从5天天气数组提取每日正午时段数据?
Hey there! Great question—you absolutely can either filter your existing 3-hour forecast data to grab noon-specific entries, or adjust your API request to directly fetch daily noon (or daily summary) data instead. Let’s break this down step by step:
1. 直接请求每日正午/汇总数据(推荐)
Most weather APIs (like the one powering your 3-hour forecast) offer a daily forecast endpoint that returns one entry per day, including pre-calculated daily high temperatures, daytime weather icons, and more. If your API supports this, modifying your request to use the daily endpoint instead of the 3-hour one is the cleanest solution:
- Look for parameters like
mode=dailyortype=dailyin your API docs - Many daily endpoints let you specify a preferred time (like noon) for daytime data, or they’ll automatically include the day’s peak conditions
- This eliminates the need to process 3-hour data entirely, which is more efficient and less error-prone
2. 从现有3小时数据中过滤正午条目
If you need to stick with your current 3-hour data for now, you can use DateFormatter (plus Calendar for better accuracy) to filter entries that fall at noon. Here’s how to adjust your existing code logic:
First, let’s refine your date handling to check for noon hours:
// Reuse your existing DateFormatter (or configure a shared one) private let dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" formatter.timeZone = TimeZone.current // Match user's local time or API's time zone return formatter }() // Filter 3-hour entries to get only noon ones func getNoonWeatherEntries(from threeHourEntries: [WeatherEntry]) -> [WeatherEntry] { let calendar = Calendar.current return threeHourEntries.filter { entry in guard let entryDate = dateFormatter.date(from: entry.dateString) else { return false } // Extract the hour from the date (using local time) let hour = calendar.component(.hour, from: entryDate) // Return true if this entry is at noon return hour == 12 } }
Bonus: Grab daily high temperatures if noon data is missing
If your 3-hour data doesn’t have an exact 12:00 entry (e.g., it returns 11:00, 14:00 instead), you can group entries by day and calculate the daily high:
func getDailyHighTemperatures(from threeHourEntries: [WeatherEntry]) -> [String: Double] { let calendar = Calendar.current var dailyHighs = [String: Double]() for entry in threeHourEntries { guard let entryDate = dateFormatter.date(from: entry.dateString), let temp = entry.temperature else { continue } // Create a key for the date (e.g., "2024-05-20") let dayKey = dateFormatter.string(from: calendar.startOfDay(for: entryDate)) // Update the high temp for this day if let currentHigh = dailyHighs[dayKey] { dailyHighs[dayKey] = max(currentHigh, temp) } else { dailyHighs[dayKey] = temp } } return dailyHighs }
3. 禁用3小时更新
To stop fetching 3-hour data entirely, you’ll need to modify your API request to use the daily forecast endpoint instead (as mentioned in point 1). There’s no client-side "toggle" to disable 3-hour granularity—you have to adjust the request parameters your app sends to the weather API.
Just a quick note: Always double-check time zones! Make sure your DateFormatter uses the same time zone as the API returns (or the user’s local time) to avoid filtering the wrong entries.
内容的提问来源于stack exchange,提问作者Rich J

