从对象信息数组中获取核心数据对象

时间:2020-10-22 21:07:48

标签: swift swiftui

我正在运行一个函数,以填充费用清单。我需要运行功能以使用.onAppear(perform: fetchRows)填充列表,以便列表在视图显示后立即更新。

每个列表项也是一个按钮。单击后将显示带有费用信息的工作表。如果需要,可以对其进行编辑。我需要的是此表更新Expense Core Data Entity实体。

工作表需要一个Expenses对象,但是我的列表由ExpensesList数组填充。如何从我的ExpensesList数组中为表单提供Expenses对象?如果有帮助的话,我在ExpensesList数组中有Expenses对象的ID。

import SwiftUI
import CoreData

@available(iOS 14.0, *)
struct ExpenseList: View {
    
    @State var editExpense: Expenses?
    @State var addExpense = false
    
    @Environment(\.managedObjectContext)
    var context: NSManagedObjectContext
    
    @FetchRequest(fetchRequest: Expenses.expensesList)
    private var result: FetchedResults<Expenses>

    @State var expensesList: [ExpensesList]?
    
    init(predicate: NSPredicate?, sortDescriptor: NSSortDescriptor) {
        let fetchRequest = NSFetchRequest<Expenses>(entityName: Expenses.entity().name ?? "Expenses")
        fetchRequest.sortDescriptors = [sortDescriptor]

        if let predicate = predicate {
            fetchRequest.predicate = predicate
        }
        _result = FetchRequest(fetchRequest: fetchRequest)
    }
    
    
    
    var body: some View {
        
        NavigationView{
            VStack{
                    
                    if expensesList != nil{
                        
                        
                        List{
                            
                            ForEach(self.expensesList!) { log in
                                Button(action: {
                                    self.editExpense = log   <--- Cannot assign value of type 'ExpensesList' to type 'Expenses'
                                }) {
                                    ExpenseRow(name: log.expense, date: log.date, amount: log.cost, account: log.account)
                                }
                            }.onDelete(perform: onDelete)
                        }
                    }
                }.navigationTitle("Expense List")
                
                .navigationBarItems(trailing:
                                        Button(action: {
                                            self.addExpense.toggle()
                                        }, label: {
                                            Image(systemName: "plus.circle.fill")
                                        }))
                
                .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)
                    
                    
                    
                }
                

        }.onAppear(perform: fetchRows)
        
                .background(EmptyView()
                                .sheet(isPresented: $addExpense, content: {
                                    ExpenseDetail(context: context)
                                }))
        

        
        
        
    }
    
    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)
            })
        }
    }
    

    private func onDelete(with indexSet: IndexSet) {
        indexSet.forEach { index in
            let log = result[index]
            context.delete(log)
        }
        try? context.saveContext()
    }
    
}

struct ExpensesList: Identifiable, Equatable {
    let id: UUID
    let expense: String
    let cost: Double
    let account: String
    let date: Date
}

extension View {
    func Print(_ vars: Any...) -> some View {
        for v in vars { print(v) }
        return EmptyView()
    }
}

0 个答案:

没有答案