What I want to do: pass a reference to a NSManagedObject of a FetchRequest (here from: TestView) to another Child-View (here: LogRectangle).
What I tried: These are basically the important lines:
List(testObjects, id: \.self) { habit in
LogRectangle(testObject: testObject)
}
And this is the whole code:
struct TestView : View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: TestObject.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \TestObject.name, ascending: true)]
) var testObjects: FetchedResults<TestObject>
var body: some View {
NavigationView {
HStack {
Button(action: {
let testObject = TestObject(context: self.managedObjectContext)
habit.name = "TestObject String"
do {
try self.managedObjectContext.save()
} catch {
// handle the Core Data error
}
}) {
Text("Insert example TestObject")
}
List(testObjects, id: \.self) { habit in
LogRectangle(testObject: testObject)
}
}
.navigationBarTitle("Test")
}
}
}
struct LogRectangle : View {
var habit : TestObject
var body: some View {
Text(habit.name)
.font(.title)
.foregroundColor(.white)
}
}
Here I get this error on the line with the text in the class LogRectangle.
Cannot convert value of type 'String?' to expected argument type 'LocalizedStringKey'
