gpt4 book ai didi

Swift - Calendar date 1 inSameDayAs date 2 for dates timestamps 返回只有一个日期的数组和重复多少次

转载 作者:行者123 更新时间:2023-11-28 14:00:05 26 4
gpt4 key购买 nike

我有 [Int64:[String:String]] 其中 Int64 是时间戳。如何检测和删除 [String:String] 中的参数之一是 ["name"] = "test" 并重复多次的同一天日期在字典中添加 ["times"] 的重复次数。

示例 [Int64:[String:String]]

    [1543058992:["name:"test"], 1543058988:["name:"test"],
1543058990:["name:"test"], 1543058984:["name:"test2"],
1543945308:["name:"test2"], 1543945208:["name:"test2]",
1550058984:["name:"test3"]]

预期 [Int64:[String:String]] 处理后:

    [1543058992: ["name:"test", "times":"3"],
1543058993: ["name:"test2", "times":"1"],
1543945308: ["name:"test2", "times":"2"],
1550058984: ["name:"test3"]]

最佳答案

import Foundation
typealias YourDict = [Int64: [String: String]]
let initial: YourDict = [
1543058992: ["name": "test"],
1543058988: ["name": "test"],
1543058990: ["name": "test"],
1543058984: ["name": "test"],
1543945308: ["name": "test2"],
1543945208: ["name": "test2"],
1550058984: ["name": "test3"]
]

var result: YourDict = [:]
let calendar = Calendar.current
initial.forEach { item in
let key = item.key
let currentValueDict = item.value
var foundCheckingKey: Int64?
let date = Date(timeIntervalSince1970: TimeInterval(key * 1000))
if result.contains(where: { checkingItem in
let checkingKey = checkingItem.key
foundCheckingKey = checkingKey
let checkingDate = Date(timeIntervalSince1970: TimeInterval(checkingKey * 1000))
return calendar.isDate(date, inSameDayAs: checkingDate)
}) {
let previousCount = Int(result[foundCheckingKey!]!["times"]!)!
result[foundCheckingKey!]!["times"] = "\(previousCount + 1)"
} else {
result[key] = ["name": currentValueDict["name"]!, "times": "1"]
}
}
print(result)

关于Swift - Calendar date 1 inSameDayAs date 2 for dates timestamps 返回只有一个日期的数组和重复多少次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53641067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com