在5天天气阵列中,如何每天使用swift隔离一个时隙? [英] In a 5 day weather array how does one isolate a single time slot per day using swift?

查看:73
本文介绍了在5天天气阵列中,如何每天使用swift隔离一个时隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当天最高峰的高温,白天图标等,这将是中午。我认为这是通过DateFormatter完成的,这是我到目前为止的工作,它返回5天的预测没问题,但是每3小时更新一次。反正有没有禁用此功能和/或仅使用此代码在中午12:00拨打电话?

I am trying to get just the high temp, day icon etc for the peak of the day, which would be noon. I think this is done through the DateFormatter, here is what I have so far and it is returning the 5 day forecast no problem, but it updates every 3 hours. Is there anyway to disable this and/or just make a call for just 12:00 noon using this code?

func getDayOfWeek(today:String)->String? {
    let formatter  = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let todayDate = formatter.date(from: today)
    formatter.dateFormat = "EEEE" // "eeee" -> Friday
    let weekDay = formatter.string(from: todayDate!)

    return weekDay
}

所有天气数据:

{
    "cod": "200",
    "message": 0.0054,
    "cnt": 40,
    "list": [{
                "dt": 1525100400,
                "main": {
                    "temp": 12.43,
                    "temp_min": 10.44,
                    "temp_max": 12.43,
                    "pressure": 1015.9,
                    "sea_level": 1035.33,
                    "grnd_level": 1015.9,
                    "humidity": 64,
                    "temp_kf": 1.99
                },
                "weather": [{
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }],
                "clouds": {
                    "all": 0
                },
                "wind": {
                    "speed": 4.66,
                    "deg": 307.008
                },
                "sys": {
                    "pod": "d"
                },
                "dt_txt": "2018-04-30 15:00:00"
            }


推荐答案

这是一个非常简单的示例点仅显示相关数据。

This is a very simple example as starting point showing only the relevant data.

该示例将 list 数组解码为自定义结构。 dt 密钥被解码为 Date 。然后,它创建一个 DateComponents 实例,该实例在当前时区中午,并过滤日期。

The example decodes the list array into a custom struct. the dt key is decoded into Date. Then it creates a DateComponents instance with noon in the current time zone and filters the dates.

struct WeatherData : Decodable {
    let cod : String
    let list : [List]
}

struct List : Decodable {
    let dt : Date
}







let jsonString = """
{ "cod": "200", "message": 0.0054, "cnt": 40, "list": [{ "dt": 1525100400, "main": {"temp": 12.43, "temp_min": 10.44, "temp_max": 12.43, "pressure": 1015.9, "sea_level": 1035.33, grnd_level": 1015.9, "humidity": 64, "temp_kf": 1.99 }, "weather": [{"id": 800, "main": "Clear", "description": "clear sky", "icon": "01d"}],    "clouds": {"all": 0}, "wind": {"speed": 4.66, "deg": 307.008},"sys": {"pod": "d"},"dt_txt": "2018-04-30 15:00:00"}]}
"""







let data = Data(jsonString.utf8)
do {
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .secondsSince1970
    let result = try decoder.decode(WeatherData.self, from: data)
    let utcDifference = TimeZone.current.secondsFromGMT() / 3600
    let noonComponents = DateComponents(hour: 12 + utcDifference, minute: 0, second: 0)
    let noonDates = result.list.filter { Calendar.current.date($0.dt, matchesComponents:noonComponents) }
    print(noonDates)

} catch {
    print("error:", error)
}

这篇关于在5天天气阵列中,如何每天使用swift隔离一个时隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆