Properly, it’s getting mannequin inheritance at the least! On this article, you’ll get a fast recap on the options of SwiftData in iOS 26, the place it used to fall quick when coping with object oriented programming paradigms, and the way modifications in WWDC 2025 helped resolve these shortcomings. First, let’s shortly go over some object oriented fundamentals.
OOPS, I don’t wish to copy code once more!
When growing with object oriented languages like Swift and Java, you may affiliate your class in two alternative ways: composition and inheritance.
Composition means that you can construct your lessons out of different lessons (and fundamental sorts). The composing class has entry to any publicly out there properties or strategies of the composed class, which may be fairly highly effective if the APIs are properly outlined. Nevertheless, something with privateness that isn’t public are hidden to the caller. With composition, the composed lessons aren’t essentially associated to the composing class, besides that one sort is used to construct, or compose, the opposite.
Inheritance however, means that you can assemble lessons by extending and inheriting properties and strategies from a father or mother class. This hierarchy enable for a extra direct connection between lessons, with dad and mom passing down properties and features to their kids. This reduces code duplication because you solely must outline shared properties and strategies within the father or mother class.
Wait, are you a mannequin?
Since its introduction, SwiftData has allowed builders to compose their SwiftData sorts out of different sorts, and even make relationships with the @Relationship macro. This in fact represents the composition type of object oriented programming mentioned above. To see this in motion, open up the starter mission for this text. In that mission, there’s a Cookbook mannequin contained in the Cookbook.swift file that’s composed of Recipes:
@Mannequin
class Cookbook {
@Relationship var recipes: [Recipe]
init(recipes: [Recipe]) {
self.recipes = recipes
}
}
The Cookbook can entry the recipes, however solely the general public a part of the Recipe API, and doesn’t get something added to its personal API free of charge. If you happen to run the starter mission now within the simulator, you’ll the very beginnings of an app that exhibits the entire recipes.
Inheriting from Previous WWDCs
Annually, SwiftData has been including increasingly more capabilities, persevering with to convey it inline with CoreData. Since some builders favor to implement broad, generic base lessons, and use inheritance to outline extra particular youngster lessons, SwiftData wanted some updates to come back inline with fashionable object oriented practices. Fortunately, at WWDC 2025 Apple launched some nice, largely clear, additions to SwiftData that present these updates!
Earlier than we dive in, a fast refresher about how SwiftData is very easy to implement. SwiftData depends closely on Swift Macros, which cover lots of implementation particulars from you, the developer, whereas offering lots of steerage to the compiler. The Cookbook instance above has a @Mannequin Swift macro earlier than the category declaration. At its most simple degree, that’s *all* it’s essential to conform a category to SwiftData! Fairly wonderful! What about in case your class makes use of inheritance?
Let’s say you’ve a generic recipe class that you’ve got setup for SwiftData already. Within the starter mission, check out the Recipe.swift file:
@Mannequin
class Recipe {
var title: String
var servings: Int
init(title: String, servings: Int) {
self.title = title
self.servings = servings
}
//... pattern knowledge is right here
}
This class has a couple of properties listed right here, one for the title, and one other for what number of servings it makes. If you happen to needed to specify extra particular forms of recipes, these could be properties you might repeat, however why try this when you may inherit them!
This yr at WWDC, SwiftData was up to date to permit for mannequin inheritance when developing your SwiftData sorts. This function is simply out there in iOS 26 and above, so any makes use of of this new API need to be annotated with an @out there test:
@out there(iOS 26, *)
This sadly signifies that in case your minimal goal is sooner than iOS 26, you received’t ready to make use of mannequin inheritance – so plan accordingly! If utilizing mannequin inheritance is essential to your program construction, you will have everybody to improve to iOS 26 (or above) earlier than utilizing the following model of your app.
The beauty of the addition of mannequin inheritance is that moreover that availability test, for fundamental utilization, you don’t need to do something particular!
When you have a extra particular sort of Recipe, for instance a Beverage, you should utilize mannequin inheritance to implement it. Make a brand new file referred to as Beverage.swift and add the next code:
@out there(iOS 26, *)
@Mannequin
class Beverage: Recipe {
var caffienated: Bool
init(title: String, servings: Int, caffienated: Bool) {
self.caffienated = caffienated
tremendous.init(title: title, servings: servings)
}
static let sampleBeverageData: [Beverage] = [
Beverage(
title: "Iced Mint Lemonade",
servings: 10,
caffienated: false,
),
Beverage(
title: "Classic Hot Chocolate",
servings: 1,
caffienated: false,
),
Beverage(
title: "Chai Latte",
servings: 1,
caffienated: true,
),
Beverage(
title: "Peach Iced Tea",
servings: 15,
caffienated: false,
)
]
}
The traditional @Mannequin Swift Knowledge macro tells the compiler that it is a Swift Knowledge mannequin, however the addition of the @out there test alerts that the macro has capabilities solely out there beginning in iOS 26, and the Beverage class is inheriting options from the father or mother mannequin, Recipe. Which means that it robotically will get the title and servings properties from the father or mother Recipe class, with out having to repeat these properties right here. No code duplication to be discovered right here!
However how does this assist us? There’s a Picker on the prime of the checklist that at present simply has “All”. It is perhaps an excellent widget to filter the forms of recipes. You possibly can add a brand new Beverage filter sort within the ContentView.swift file:
enum RecipeType: String, CaseIterable, Identifiable {
case all = "All"
case drinks = "Drinks"
var id: String { self.rawValue }
}
You may as well add within the recipes to the context when the app begins, proper beneath the place the recipes are added:
for beverage in Beverage.sampleBeverageData {
modelContainer.mainContext.insert(beverage)
}
Additionally, don’t overlook so as to add the Beverage mannequin to the out there schemas on the prime of that file:
let schema = Schema([ Cookbook.self, Recipe.self, Beverage.self ])
If you happen to run the app within the simulator now, you’ll see that the entire recipes present up, however that the picker doesn’t do something particular. It is because some smarts should be added for the filtering course of. See the the place to go subsequent part beneath for subsequent steps right here.
The underside line right here is that should you’re utilizing iOS 26 or above, to reap the benefits of inheritance when constructing your fashions, at it most simple, you must do nothing new moreover an @out there test. That is the ability of Swift Macros in motion!
