I'm running a function in order to populate a list of expenses. I need to run the function to populate the list using .onAppear(perform: fetchRows)
so that the list updates as soon as the the view appears.
Xcode doesn't like the fact that I'm running a function to populate expensesList. How can I make this work? Is there a better way of going about this?
@State var expensesList: [ExpensesList]?
var body: some View {
NavigationView{
VStack{
if expensesList != nil{
List{
ForEach(self.expensesList!) { <-- Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
(log: ExpensesList) in {
Button(action: {
self.editExpense = log
})
ExpenseRow(name: log.expense, date: log.date, amount: log.cost, account: log.account)
}
}.onDelete(perform: onDelete)
}
}
}.navigationTitle("Expense List")
struct ExpensesList: Identifiable, Equatable {
let id: UUID
let expense: String
let cost: Double
let account: String
let date: Date
}
func fetchRows(){
Expenses.fetchAllExpensesInDateRange(context: self.context) { (results) in
guard !results.isEmpty else { return }
self.expensesList = results.map({ (result) -> ExpensesList in
print(result.id,result.expenseName,result.expenseCost, result.expenseAccount, result.expenseDate)
return ExpensesList(id: result.id, expense: result.expenseName, cost: result.expenseCost, account: result.expenseAccount, date: result.expenseDate)
})
}
}
The sheet I use to edit expenses is here. As you can see I use a Core Data entity called Expenses to store my expense objects.
.sheet(item: $editExpense, onDismiss: {
self.editExpense = nil
})
{
(log: Expenses) in
ExpenseDetail(
context: self.context,
editExpense: log,
name: log.expenseName ?? "",
amount: String(log.expenseCost),
category: log.expenseCategory ?? "",
date: log.expenseDate ?? Date(),
account: log.expenseAccount ?? "",
isMonthly: log.expenseIsMonthly
).environment(\.managedObjectContext, self.context)
}