Reputation: 34798
Any ideas why I am getting this error here? I haven't had this in the past with Core Data on a separate test project.
Cannot convert value of type 'FetchedResults' to expected argument type 'Range'
SwiftUI View Code (have marked error below)
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) var context
@FetchRequest(entity: GCItem.entity(), sortDescriptors: []) var gcItems: FetchedResults<GCItem>
private func addItem(title:String) {
let newItem = GCItem(context: context)
newItem.id = UUID()
newItem.title = title
do {
try context.save()
} catch let e as NSError {
fatalError("Unresolved error \(e), \(e.userInfo)")
}
}
var body: some View {
NavigationView {
VStack {
List() {
ForEach(gcItems) { gcItem in // ** ERROR HERE ***
HStack {
Text("test")
}
}
}
Button(action: { self.addItem(title: "Testing 123") }) {
Text("ADD ITEM")
}
}
}
}
}
Visual Image of Error
Core Data Model
Core Data File
Upvotes: 2
Views: 5165
Reputation: 4503
GCItem
needs to conform to Identifiable
for that code to work. A simple empty conformance should work, as it already has an id
:
extension GCItem: Identifiable { }
Upvotes: 4