import Foundation
class Spending {
let category: String
let sum: Int
let day: Int
init(category: String, sum: Int, day: Int) {
self.category = category
self.sum = sum
self.day = day
}
}
struct CellData {
let category: String
let sum: Int
}
class DaySection {
var cells = [CellData]()
let day: Int
init(day: Int, cell: CellData) {
self.cells.append(cell)
self.day = day
}
}
var destArr = [DaySection]()
var sourceArr = [Spending]()
sourceArr.append(Spending(category:"1", sum:1, day:1))
sourceArr.append(Spending(category:"2", sum:2, day:1))
sourceArr.append(Spending(category:"1", sum:1, day:2))
sourceArr.append(Spending(category:"2", sum:2, day:2))
sourceArr.append(Spending(category:"1", sum:1, day:3))
destArr = sourceArr.reduce([DaySection](), { (aggr, elem) in
let newCell = CellData(category: elem.category, sum: elem.sum)
guard let day = aggr.first(where: {$0.day == elem.day})
else {
var temp = aggr
let newDaySection = DaySection(day: elem.day, cell: newCell)
temp.append(newDaySection)
return temp
}
day.cells.append(newCell)
return aggr
})
</spoiler>