gpt4 book ai didi

swift - 如何将对象数组分组并转换为另一个分组对象数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:54:01 24 4
gpt4 key购买 nike

假设我有一组 Report 对象和 GroupedReport 对象这是我的报表类结构

class Report {

var report_date:String
var problem:String
var description:String
var is_know:Int
var is_solved:Int

init(report_date:String,problem:String,description:String,is_know:Int,is_solved:Int) {
self.report_date = report_date
self.problem = problem
self.description = description
self.is_know = is_know
self.is_solved = is_solved
}
}

这是我的 GroupedReport 类结构

        class GroupedReport{
var problem:String
var report_array:[Report]

init(problem:String){
self.problem = problem
report_array = []
}

public var description: String { return "GroupedReort:\nProblem:\(self

.problem)\nArray:\(self.report_array)" }
}

有没有什么算法可以将具有相同问题(Report类变量)值的Report对象分组,转化为GroupedReport对象?我已经完成了自己的实现,但我的代码没有按预期工作。有人可以帮我吗?感谢您的关注

            var reports:[Report] = []
var problem_array:[String] = []
var grouped_reports: [GroupedReport] = []
func group_array(){
for i in 0...self.reports.count-1{
print("\(i) Loop ::")
print("============")
var problem_found:Bool = false
// j loop start
if problem_array.count > 0 {
for j in 0...self.problem_array.count-1{
print("problem_array[j]: \(problem_array[j]) <compare to> reports[i].problem: \(reports[i].problem)")
print("")
if(problem_array[j] == reports[i].problem){
print("")
print("problem_array[j] \(problem_array[j]) <is equal to> reports[i].problem \(reports[i].problem)")
problem_found = true
// this is the existing problem
}
}
}
// j loop end
if(problem_found){
//find problem key and append array to that key
for x in 0...self.grouped_reports.count-1{
if self.grouped_reports[x].problem == reports[x].problem{
print("")
print("Problem found")
print("Append Report problem :\(reports[x].problem)")
print("Append Report des :\(reports[x].description)")
self.grouped_reports[x].report_array.append(reports[x])
}
}
}
else{
// create new problem key
problem_array.append(reports[i].problem)
//crete new group_report with new problem and append current report[i] to that report , append that group_report to the group_report array
var group_report = GroupedReport(problem: reports[i].problem)
group_report.report_array.append(reports[i])
print("")
print("new problem")
print("Append Report problem :\(reports[i].problem)")
print("Append Report des :\(reports[i].description)")

self.grouped_reports.append(group_report)

}

}
print("!!Final Array!!")
print("=================")

for i in 0...grouped_reports.count-1 {
print("\(i) Array!")
print("-----------")
print("Problem:\(self.grouped_reports[i].problem)")
print("Inner Array")
print("count: \(grouped_reports[i].report_array.count)")
for j in 0...grouped_reports[i].report_array.count-1{
print(grouped_reports[i].report_array[j].description)
}
}
}

最佳答案

使用这样的代码生成分组报告:

var groupedReports: [String: GroupedReport] = [:]

reports.forEach { report in
if let groupedReport = groupedReports[report.problem] {
groupedReport.report_array.append(report)
} else {
let groupedReport = GroupedReport(problem: report.problem)
groupedReport.report_array.append(report)
groupedReports[report.problem] = groupedReport
}
}

然后你可以像这样遍历结果:

for (problem, groupedReport) in groupedReports {
// do something with the groupedReport
}

更新

或者像这样将结果转换成数组:

let grouped_reports = Array(groupedReports.values)

关于swift - 如何将对象数组分组并转换为另一个分组对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233779/

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