Hi.
Maybe it's elementary, but I can't figure out how to do it. After studying the One-to-many relationships with Core Data lesson, I created a project by slightly changing the display of the view and adding a window for creating a new candy.
How can I save this candy to the country I went to via the NavigationLink?
List {
ForEach(countries, id: \.self) { country in
NavigationLink(country.wrappedFullName) {
Form {
ForEach(country.candyArray,id: \.self) { candy in
Text(candy.wrappedName)
}
Button("Add Candy") {
presentingAddNewCandySheet.toggle()
}
}
}
}
}
I create a new candy through
TextField("Candy name", text: $name)
The data is saved and a new candy appears, but a new country is also created
Button("Add") {
let candy = Candy(context: moc)
candy.name = name
candy.origin = Country(context: moc) /// ?
candy.origin?.shortName = "UK" /// ?
candy.origin?.fullName = "United Kingdom" /// ?
try? moc.save()
}
What should be instead of these lines?
Thanks for the possible answers!