0
はのは、私がレポートオブジェクトの配列を持っているとGroupedReportこれはこれは私の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
}
}
ある をオブジェクトとしましょうします
同じ問題(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)
}
}
}
私はインデックス値でこの辞書にアクセスすることができますどのように私は最初 – simonWasHere
おかげsir..youコード実際に動作する!!でオフ逃した最初の行を追加して編集しました。 (viewForHeaderInSectionメソッドとcellForRowAt indexPathメソッド)。 –
あなたが書くような簡単なアクセスのための配列で結果を好む場合:let reportArray = Array(indexPath.rowとindexPath.section) (groupedReports.values) – simonWasHere