如何将日期合并成字典?

时间:2018-07-23 07:57:18

标签: ios swift dictionary

我在词典方面遇到麻烦。

例如,

我当前的数据

["date": "2018-01-01", "name": "New Year"], ["date": "2018-01-31", "name": "Public holiday"],["date": "2018-02-14", "name": "Valentine's Day"], ["date": "2018-02-28", "name": "Public Holiday"]

如何转换为

["month": ["January": ["date": "2018-01-01", "name": "New Year"], ["date": "2018-01-31", "name": "Public holiday"]],["February": ["date": "2018-02-14", "name": "Valentine's Day"], ["date": "2018-02-28", "name": "Public Holiday"]]]

3 个答案:

答案 0 :(得分:2)

您可以使用DateFormatterString格式的日期(yyyy-MM-dd)读取到Date实例中,然后重新使用格式化程序来提取完整​​的月份名称( MMMM)中的日期作为您的String键。

将其与reduce(into:)合并以处理data数组:

let data = [
    ["date": "2018-01-01", "name": "New Year"],
    ["date": "2018-01-31", "name": "Public holiday"],
    ["date": "2018-02-14", "name": "Valentine's Day"],
    ["date": "2018-02-28", "name": "Public Holiday"]
]

let monthCategorizedData = data
    .reduce(into: (result: [String: [[String: String]]](),
                   formatter: DateFormatter(),
                   calendar: Calendar(identifier: .gregorian))) {(args, data) in
    var month = "Unknown"
    args.formatter.dateFormat = "yyyy-MM-dd" // format: read date from string
    if let date = data["date"].flatMap({ args.formatter.date(from: $0)}) {
        args.formatter.dateFormat = "MMMM"   // format: read month (as string) from date
        month = args.formatter.string(from: date)
    }
    args.result[month] = args.result[month, default: []] + [data]
}.result

print(monthCategorizedData)
/* ["January":  [["date": "2018-01-01", "name": "New Year"],
                 ["date": "2018-01-31", "name": "Public holiday"]],
    "February": [["date": "2018-02-14", "name": "Valentine\'s Day"],
                 ["date": "2018-02-28", "name": "Public Holiday"]]] */

如果数据的日期无效(String格式的Date失败,或者"date"中不存在data键),以上内容会将它们分类为键Unknown。如果您只想丢弃此类数据(这样的失败应该是意外的),则可以将以上内容修改为:

let monthCategorizedData = data
    .reduce(into: (result: [String: [[String: String]]](),
                   formatter: DateFormatter(),
                   calendar: Calendar(identifier: .gregorian))) {(args, data) in
    args.formatter.dateFormat = "yyyy-MM-dd" // format: read date from string
    if let date = data["date"].flatMap({ args.formatter.date(from: $0)}) {
        args.formatter.dateFormat = "MMMM"   // format: read month (as string) from date
        let month = args.formatter.string(from: date)
        args.result[month] = args.result[month, default: []] + [data]
    }
}.result

将结果进一步嵌套一层到"month"中的[String: [String : [[String : String]]]]键的值中是很直接的:

let categorizedData = ["month": monthCategorizedData]

print(categorizedData)
/* ["month": ["January":  [["date": "2018-01-01", "name": "New Year"],
                           ["date": "2018-01-31", "name": "Public holiday"]],
              "February": [["date": "2018-02-14", "name": "Valentine\'s Day"],
                           ["date": "2018-02-28", "name": "Public Holiday"]]]] */

答案 1 :(得分:1)

虽然我不是专家,但对我来说问题似乎就在于你如何获得 “ date” =“ year-month-day”月的值 如果你能够削减多少呢?或仅获得第5和第6个字符,它将成为您需要的月份数 基于您可以使用if或switch并制作一个字典数组 甚至只是字典,然后您可以根据需要将其添加到数组中。

因此在获得本月编号后,将是这样:

var dict:[String:[[String:String]]] = [:]

dict [“ janury”] = [[“ 2012-01-12”:“事件”],[“ 2012-01-13”:“ event2”]]

如果monthNumber == 1 {

dict["janury"]?.append(["2012-01-14":"event3"])

}

然后每个月,如果总共12个最终切换为12条语句,则需要新的

关于如何获得这个字符串的值,我还不知道,但是我几乎可以肯定,谷歌可以帮助它或者比我的经验更好的人

答案 2 :(得分:1)

Dictionary有一种方法Screenshot,可以通过一种非常聪明的方式做到这一点。

然后将date参数转换为Date,然后转换为带有DateFormatter的月份名称

let array = [["date": "2018-01-01", "name": "New Year"],
             ["date": "2018-01-31", "name": "Public holiday"],
             ["date": "2018-02-14", "name": "Valentine's Day"],
             ["date": "2018-02-28", "name": "Public Holiday"]]

let dictionary = Dictionary(grouping: array) { (item) -> String in
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    guard let dateString = item["date"],
        let date = formatter.date(from: dateString) else  { return "Unknown" }
    formatter.dateFormat = "MMMM"
    return formatter.string(from: date)
}

print(["months" : dictionary])