作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 [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/
我有 [Int64:[String:String]] 其中 Int64 是时间戳。如何检测和删除 [String:String] 中的参数之一是 ["name"] = "test" 并重复多次的同一天
我是一名优秀的程序员,十分优秀!