0

因此,我尝试使用以下代码对 ForEach 列表中的值求和:

@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Transaction.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Transaction.value, ascending: false)]) var fetchRequest: FetchedResults<Transaction>

var sum: Double {
    fetchRequest.reduce(0) { $0 + $1.value } //ERROR! 
    } 

每次我运行时,都会出现一个错误:

[SwiftUI] 环境中的上下文未连接到持久存储协调器:<NSManagedObjectContext:0x2813f2920>

使用 PO:

(lldb) po fetchRequest
error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated

这是其余的代码:

var body: some View {
    NavigationView {
            Form {
                ...
                            Text("\(sum)")
                        }
                }
                ...

你能帮我找出错误吗?

4

0 回答 0