I am relatively new to SwiftUI and would like to have a view where the user selects a result from a CoreData FetchRequest in a Picker. I have the following lines inside the view struct but outside the view body,
@FetchRequest(
entity: Person.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Person.name, ascending: true)
]
) var people: FetchedResults<Person>
@State var currentPerson:Person
Inside the view body, I have got a Picker that saves the selected value to currentPerson but when I try to run the program in the simulator, I get an error that states
Missing argument for parameter 'currentPerson' in call
at the preview struct at the end of the file.
If I try to initialise currentPerson with the first item in people I get a error that states Cannot use instance member 'people' within property initializer; property initializers run before 'self' is available.
Apart from passing a Person object into the view, the other solution that I have had work is the create an object to initialise currentPerson variable is to create a Person object but not pass the Managed Object Context as the parameter.
However when I run the program it shows CoreData: error: Failed to call designated initializer on NSManagedObject class 'Person' in the console, but the program runs as intended.
Is this the best way to do this or am I missing something that would work better? Thanks!
@State var currentPerson : Person? = nil- jnpdxas Person?to the Picker tag and some checks for if thecurrentPersonvariable was stillnil. - brownandrew15