So I’m still learning SwiftData and the one thing I cannot get my head around is how relationships don’t cause infinite loops.
For example, using SwiftData, if you want to list all the films an actor is in, you would probably do something like this:
@Model
class Actor {
var name: String
@Relationship var movies: [Movies]
init(name: String, movies: [Movies]) {
self.name = name
self.movies = movies
}
}
@Model
class Movies {
var title: String
var actor: Actor
init(title: String, actor: Actor) {
self.title = title
self.actor = actor
}
}
But I can’t understand how to create a new Actor/Movie object. So if I want to create a new Actor object, the initialiser says I need to create a new Movies object. But the initialiser for Movies says I need to create a new actor object, and so on.
Can someone explain how Relationships like this are meant to work?
sam0701 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.