ios – SwiftData multiple relationships to one object


I’m trying to create an app with SwiftData that tracks TV shows. There is a Show, Season, and Episode object.

@Model
class Show {
    @Attribute(.unique) let id: Int
    @Relationship(inverse: \Season.show) var season: [Season]
    var nextEpisode: Episode?
    var lastEpisode: Episode?
}

@Model
class Season {
    @Attribute(.unique) let id: Int
    @Relationship(inverse: \Episode.season) var episodes: [Episode]
    var show: Show?
}

@Model
class Episode {
    @Attribute(.unique) let id: Int
    var season: Season?
}

How can I create a relationship between the show and episode for the nextEpisode and lastEpisode properties?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img