Search code examples
swiftuicore-data

Error "No exact matches in reference to static method 'buildExpression'" when trying to update CoreData item


Currently playing around with iPhone development for the first time in quite a while and have a hurdle I can't seem to get over. The idea is that this code will allow me to update item.count when I am ready to save. At this time I have to calculate the new value. In this code example, I am simply setting it to 0.

This is generating the following error for the line item.count = 0:

No exact matches in reference to static method 'buildExpression'

The following is the code:

import SwiftUI
import CoreData
      
struct CollectedFormView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: true)],
        predicate: NSPredicate(format: "count > %i", Int32(0)),
        animation: .default
    ) private var items: FetchedResults<Item>

    var body: some View {
        VStack {
            Form {
                ...
                }
            }
            .toolbar {
                ToolbarItem {
                    Button("Save") {
                        saveItems()
                    }
                }
            }.navigationTitle("Collected Items")
        }
    }
    
    private func saveItems() {
        ForEach(items) { item in
            if (collected[item.name ?? ""] != nil) {
                item.count = 0
            }
        }
        ...
    }
}

I have removed any code that I do not believe is causing an issue. Can anyone point me in the right direction as to what might be causing this error?


Solution

  • ForEach(...) is a view in SwiftUI which needs to return a view, that is why you have the error. So try the array loop, items.forEach{ item in ... instead