0Pricing
Swift Academy · Lesson

Relationships and Fetch Descriptors

Modelling one-to-many relationships and querying with FetchDescriptor.

SwiftData Relationships

Declare relationships between @Model classes using Swift properties. SwiftData infers the relationship type.

@Model
class Author {
  var name: String
  @Relationship(deleteRule: .cascade) var books: [Book] = []
  init(name: String) { self.name = name }
}

@Relationship Attribute

Use @Relationship to specify delete rules and inverse relationships explicitly.

@Model
class Book {
  var title: String
  var author: Author?
  init(title: String) { self.title = title }
}

All lessons in this course

  1. Core Data Stack: NSPersistentContainer Setup
  2. SwiftData @Model and ModelContext
  3. Relationships and Fetch Descriptors
  4. Lightweight Migrations and CloudKit Sync
← Back to Swift Academy