SwiftData in iOS 26

Dec 10 2025 · Swift 6.2, iOS 26, macOS 26, Xcode 26

Lesson 02: Model Inheritance

SwiftData Model Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last segment, you setup the necessary SwiftData @Models to include Beverages and BakedGoods, both of which are children of the parent Recipe class. The question remains, do you really need to use inheritance?

private func insertSampleData() {
  for recipe in Recipe.sampleData {
    context.insert(recipe)
  }

  for bakedGood in BakedGood.sampleBakedGoodData {
    context.insert(bakedGood)
  }

  for beverage in Beverage.sampleBeverageData {
    context.insert(beverage)
  }
}
let schema = Schema([
    Recipe.self, Ingredient.self, Cookbook.self, BakedGood.self,
      Beverage.self
  ])
var body: some Scene {
  WindowGroup {
    ContentView()
      .modelContainer(for: [Recipe.self, Ingredient.self, Cookbook.self,
        BakedGood.self, Beverage.self])
  }
}
@State private var recipeType: RecipeType = .all
enum RecipeType: String, CaseIterable, Identifiable {
	case all = "All"
  case bakedGoods = "Baked Goods"
  case beverages = "Beverages"

  var id: String { self.rawValue }
}
Picker("Recipe Type", selection: $recipeType) {
	ForEach(RecipeType.allCases) { recipeType in
	  Text(recipeType.rawValue)
	    .tag(recipeType)
	}
}
.pickerStyle(SegmentedPickerStyle())
@Binding var recipeType: RecipeType
RecipeListView(recipeType: $recipeType)
@Query var beverages: [Beverage]
@Query var bakedGoods: [BakedGood]
switch recipeType {
  case .all:
    ForEach(recipes) { recipe in
      NavigationLink(destination: RecipeDetailView(recipe: recipe)) {
        Text(recipe.name)
      }
    }
    .onDelete(perform: deleteRecipes)
  case .bakedGoods:
    ForEach(bakedGoods) { recipe in
      NavigationLink(destination: RecipeDetailView(recipe: recipe)) {
        Text(recipe.name)
      }
    }
    .onDelete(perform: deleteRecipes)
  case .beverages:
    ForEach(beverages) { recipe in
      NavigationLink(destination: RecipeDetailView(recipe: recipe)) {
        Text(recipe.name)
      }
    }
    .onDelete(perform: deleteRecipes)
}
See forum comments
Cinema mode Download course materials from Github
Previous: Inheritance vs Composition Next: Conclusion