inheritance_skill

This skill helps you design SwiftData model hierarchies with class inheritance, enabling polymorphic queries and proper schema migrations.
  • Swift

56

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill rshankras/claude-code-apple-skills --skill inheritance

  • SKILL.md10.7 KB

Overview

This skill explains class inheritance patterns for SwiftData models, focusing on when to choose inheritance vs enums or protocols. It shows how to annotate base and subclass models, configure ModelContainer, and query across hierarchies while avoiding common pitfalls. Practical guidance covers relationships, type-based filtering, and migration considerations.

How this skill works

The skill inspects model design choices and provides concrete code patterns: apply @Model to the base class and every subclass, place shared persistent properties on the base, and register only the base with ModelContainer. It describes querying the base class to return all subclasses, filtering with #Predicate using type checks, and safely casting to access subclass fields in UI and business logic. It also highlights mistakes that cause lost persisted data or migration headaches.

When to use it

  • You have a clear IS‑A relationship where subclasses add significant stored properties or behavior
  • You need both deep queries (all base instances) and shallow queries (only one subtype)
  • You need polymorphic relationships that hold mixed subtype instances
  • You are migrating a Core Data inheritance hierarchy to SwiftData
  • You want a shallow hierarchy (base + one tier) and manageable migrations

Best practices

  • Apply @Model to the base class and every subclass so subclass properties persist
  • Keep the hierarchy shallow: base class plus one level of subclasses only
  • Call super.init(...) from every subclass initializer with all required base properties
  • Register the base class in ModelContainer; SwiftData auto-discovers subclasses
  • Prefer an enum on the base model when differences are only a type tag or one/two optional fields
  • Use #Predicate and is/as? checks for type-based filtering and safe casting in views

Example use cases

  • Trip model where BusinessTrip and PersonalTrip share identity and many fields, but add distinct properties
  • A polymorphic relationship: Accommodation.trip can point to any Trip subtype at runtime
  • UI with segmented filter switching between all trips, business trips, and personal trips
  • Migrating a two-level Core Data inheritance scheme to SwiftData with minimal schema changes
  • Choosing enum on Trip for small variant differences to avoid migration complexity

FAQ

Yes. Omitting @Model on a subclass means its stored properties will not be persisted.

When should I use an enum instead of inheritance?

Use an enum on the base model when variants differ only by a type label and one or two optional fields; it simplifies schema and migrations.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational