I tried the great tutorial from Paul Hudson to have a look at the Core Data relationship: https://www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest and everything worked like a charm.
But unfortunately this isn't what I really need and when I make some changes it's not working like I'd love to!
Here's what I'm trying to make:
VStack {
ForEach(countries) { country in
HStack {
Text(country.wrappedFullName)
Spacer()
Button("Add") {
// I'd love to add a candy from here
// And based on the "id" and not the "shortName"
let candy = Candy(context: moc)
candy.id = UUID()
candy.date = Date()
candy.name = "Example"
candy.origin = Country(context: moc) // I have to repeat that?
candy.origin?.id = UUID() // This isn't right!
try? moc.save()
}
}
}
}
And then, in another screen (SwiftUI View), I list every candies from the selected country.
So what am I doing wrong? Do you know a simple solution to this problem? Thanks in advance. ✌️