Good day, everyone! I've got some entry-level question, so don't be too harsh on me.
I'm developing iExpence example project further, and I want the sum of user spendings for a week.
I've got a struct...
struct CashItem: Identifiable, Hashable {
let id = UUID()
let date: Date
let sum: Double
}
...and a published class.
class Money: ObservableObject {
@Published var cash = [CashItem]()
Now, I'm trying to build a function to filter it all. In folded function I return an array of days of current week, and then I'm trying to apply it to the filtered array.
func currentWeekCash() -> Double {
let calendar = Calendar.current
let startOfCurrentWeek = calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))
func getWeekDates(of startDate: Date, with calender: Calendar) -> [Date] {
var weekDates: [Date] = []
for i in 0..<7 {
weekDates.append(calendar.date(byAdding: .day, value: i, to: startDate)!)
}
return weekDates
}
let currentWeekDates = getWeekDates(of: startOfCurrentWeek!, with: calendar)
func targetDataArray() -> [Double] {
let targetDataArray = money.cash.map ({ $0.sum }).filter {
for i in currentWeekDates {
Calendar.current.isDate(money.cash.map ({ $0.date }), equalTo: i, toGranularity: .day)
}
}
}
let targetData = targetDataArray().reduce(0.0, +)
return targetData
}
Now, I've got a responce of Cannot convert value of type '[Date]' to expected argument type 'Date'
, which makes sense, but if I'm trying to make loop over loop, it returns me type of [()]
. It makes no sense at all.
So, I'm a little completely confused.