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)
}
You have an extra {
after (log: ExpensesList) in {
and your Button
has no content
parameter.
Try the following instead (I assume that ExpenseRow
should be the content
of the Button):
ForEach(self.expensesList!) { log in
Button(action: {
self.editExpense = log
}) {
ExpenseRow(name: log.expense, date: log.date, amount: log.cost, account: log.account)
}
}
Note that in SwiftUI 2 you can use control flow statements (like if-let
) directly in @ViewBuilder
blocks:
if let expensesList = expensesList {
List {
ForEach(expensesList) { log in
// ...
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.