cuốn sách gpt4 ai đã làm

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

In lại Tác giả: Walker 123 更新时间:2023-11-28 14:00:05 26 4
mua khóa gpt4 Nike

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

Ví dụ [Int64:[String:String]]

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

hy vọng [Int64:[String:String]] 处理后:

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

câu trả lời hay nhất

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)"
} khác {
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
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress