gpt4 book ai didi

arrays - 计算 Firebase (Swift) 的结果数

转载 作者:行者123 更新时间:2023-11-30 10:39:10 27 4
gpt4 key购买 nike

我有一个 Firebase 数据库正在运行,它存储 iOS 应用程序用户报告的数据;例如,可以报告杂货店,然后将其存储在数据库中。在商店名称旁边,报告时间(四舍五入为 15 分钟)也以 reportedTime 形式存储在数据库中。

现在我想知道一天中的哪个时间 - 即早上(06:00 - 12:00 点)、中午(12:00 - 18:00 点)、晚上(18:00 - 24:00 点)或夜间(00:00 - 06:00) 00 点) – 提交了多少份报告。

我从 firebase 获取值,将它们存储在模型中,因此可以访问,例如,report.reportTime,它返回类似“2019-07-27 19:30: 00 +0000”作为每个单独报告的字符串。

例如,如何计算 00:00:00 到 06:00:00 之间的报告数量?

我正在使用 SwiftValidators 并尝试了以下操作(不要评判我!):

if Validator.contains("00:00:00").apply(report.reportTime) || Validator.contains("00:15:00").apply(report.reportTime) || Validator.contains("00:30:00").apply(report.reportTime) || Validator.contains("00:45:00").apply(report.reportTime) || Validator.contains("01:00:00").apply(report.reportTime) || Validator.contains("01:15:00").apply(report.reportTime) || Validator.contains("01:30:00").apply(report.reportTime) || Validator.contains("01:45:00").apply(report.reportTime) || Validator.contains("02:00:00").apply(report.reportTime) || Validator.contains("02:15:00").apply(report.reportTime) || Validator.contains("02:30:00").apply(report.reportTime) || Validator.contains("02:45:00").apply(report.reportTime) || Validator.contains("03:00:00").apply(report.reportTime) || Validator.contains("03:15:00").apply(report.reportTime) || Validator.contains("03:30:00").apply(report.reportTime) || Validator.contains("03:45:00").apply(report.reportTime) || Validator.contains("04:00:00").apply(report.reportTime) || Validator.contains("04:15:00").apply(report.reportTime) || Validator.contains("04:30:00").apply(report.reportTime) || Validator.contains("04:45:00").apply(report.reportTime) || Validator.contains("05:00:00").apply(report.reportTime) || Validator.contains("05:15:00").apply(report.reportTime) || Validator.contains("05:30:00").apply(report.reportTime) || Validator.contains("05:45:00").apply(report.reportTime) {

// do something which I don't know

}

最佳答案

在模型Report中创建一个枚举TimeOfTheDay并根据小时值初始化该枚举。并在模型中创建一个 TimeOfTheDay 类型的计算属性。

struct Report {
enum TimeOfTheDay {
case morning, noon, evening, night
init?(_ hour: Int) {
switch hour {
case 0...6:
self = .night
case 6...12:
self = .morning
case 12...18:
self = .noon
case 18...24:
self = .evening
default:
return nil
}
}
}
var id: String
var reportTime: String
//...other properties
var timeOfTheDay: TimeOfTheDay? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
if let date = dateFormatter.date(from: reportTime) {
let hour = Calendar.current.component(.hour, from: date)
return TimeOfTheDay(hour)
}
return nil
}
}

选项 1

现在使用Dictionary(grouping:by:)方法,您可以根据一天中的时间值对 reports 数组进行分组。

var allReports = [Report(id: "a", reportTime: "2019-07-27 01:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 20:30:00 +0000"),
Report(id: "a", reportTime: "2019-07-27 11:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 03:30:00 +0000"),
Report(id: "a", reportTime: "2019-07-27 09:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 10:30:00 +0000"),
Report(id: "a", reportTime: "2019-07-27 15:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 07:30:00 +0000")]
var groupedReports = Dictionary(grouping: allReports) { $0.timeOfTheDay }
print(groupedReports)
//[night: [Report(id: "a", reportTime: "2019-07-27 20:30:00 +0000")],
// morning: [Report(id: "a", reportTime: "2019-07-27 01:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 03:30:00 +0000")],
// noon: [Report(id: "a", reportTime: "2019-07-27 11:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 09:30:00 +0000"),
// Report(id: "a", reportTime: "2019-07-27 10:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 07:30:00 +0000")],
// evening: [Report(id: "a", reportTime: "2019-07-27 15:30:00 +0000")]]

您可以从词典中获取仅限早上的报告

print(groupedReports[Report.TimeOfTheDay.morning])
//[Report(id: "a", reportTime: "2019-07-27 01:30:00 +0000"),Report(id: "a", reportTime: "2019-07-27 03:30:00 +0000")]

选项 2

使用filter(_:)仅获取早晨报告的方法

let morningReports = allReports.filter { $0.timeOfTheDay == Report.TimeOfTheDay.morning }
print(morningReports)
//[Report(id: "a", reportTime: "2019-07-27 01:30:00 +0000"), Report(id: "a", reportTime: "2019-07-27 03:30:00 +0000")]

关于arrays - 计算 Firebase (Swift) 的结果数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57250624/

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