假设我有一组Report Objects和GroupedReport Objects 这是我的报告类结构
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)
}
}
}
答案 0 :(得分:2)
使用这样的代码生成分组报告:
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)