View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Model your schema with SwiftData

    Learn how to use schema macros and migration plans with SwiftData to build more complex features for your app. We'll show you how to fine-tune your persistence with @Attribute and @Relationship options. Learn how to exclude properties from your data model with @Transient and migrate from one version of your schema to the next with ease.

    To get the most out of this session, we recommend first watching "Meet SwiftData" and "Build an app with SwiftData" from WWDC23.

    Capítulos

    • 0:00 - Intro
    • 1:41 - Utilizing schema macros
    • 5:30 - Evolving schemas
    • 8:56 - Wrap-up

    Recursos

    • SwiftData
    • Adopting SwiftData for a Core Data app
      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC23

    • Build an app with SwiftData
    • Discover Observation in SwiftUI
    • Dive deeper into SwiftData
    • Meet SwiftData
    • Migrate to SwiftData
  • Buscar neste vídeo...
    • 0:56 - Original Trip model

      import SwiftUI
      import SwiftData
      
      @Model
      final class Trip {
          var name: String
          var destination: String
          var start_date: Date
          var end_date: Date
          
          var bucketList: [BucketListItem]? = []
          var livingAccommodation: LivingAccommodation?
      }
    • 1:50 - Adding a unique attribute

      @Model 
      final class Trip {
          @Attribute(.unique) var name: String
          var destination: String
          var start_date: Date
          var end_date: Date
          
          var bucketList: [BucketListItem]? = []
          var livingAccommodation: LivingAccommodation?
      }
    • 2:48 - Specifying original property names

      @Model 
      final class Trip {
          @Attribute(.unique) var name: String
          var destination: String
          @Attribute(originalName: "start_date") var startDate: Date
          @Attribute(originalName: "end_date") var endDate: Date
          
          var bucketList: [BucketListItem]? = []
          var livingAccommodation: LivingAccommodation?
      }
    • 4:00 - Cascading delete rule

      @Model 
      final class Trip {
          @Attribute(.unique) var name: String
          var destination: String
          @Attribute(originalName: "start_date") var startDate: Date
          @Attribute(originalName: "end_date") var endDate: Date
          
          @Relationship(.cascade)
          var bucketList: [BucketListItem]? = []
        
          @Relationship(.cascade)
          var livingAccommodation: LivingAccommodation?
      }
    • 4:54 - Transient properties

      @Model 
      final class Trip {
          @Attribute(.unique) var name: String
          var destination: String
          @Attribute(originalName: "start_date") var startDate: Date
          @Attribute(originalName: "end_date") var endDate: Date
          
          @Relationship(.cascade)
          var bucketList: [BucketListItem]? = []
        
          @Relationship(.cascade)
          var livingAccommodation: LivingAccommodation?
      
          @Transient
          var tripViews: Int = 0
      }
    • 7:12 - Defining versioned schemas

      enum SampleTripsSchemaV1: VersionedSchema {
          static var models: [any PersistentModel.Type] {
              [Trip.self, BucketListItem.self, LivingAccommodation.self]
          }
      
          @Model
          final class Trip {
              var name: String
              var destination: String
              var start_date: Date
              var end_date: Date
          
              var bucketList: [BucketListItem]? = []
              var livingAccommodation: LivingAccommodation?
          }
      
          // Define the other models in this version...
      }
      
      enum SampleTripsSchemaV2: VersionedSchema {
          static var models: [any PersistentModel.Type] {
              [Trip.self, BucketListItem.self, LivingAccommodation.self]
          }
      
          @Model
          final class Trip {
              @Attribute(.unique) var name: String
              var destination: String
              var start_date: Date
              var end_date: Date
          
              var bucketList: [BucketListItem]? = []
              var livingAccommodation: LivingAccommodation?
          }
      
          // Define the other models in this version...
      }
      
      enum SampleTripsSchemaV3: VersionedSchema {
          static var models: [any PersistentModel.Type] {
              [Trip.self, BucketListItem.self, LivingAccommodation.self]
          }
      
          @Model
          final class Trip {
              @Attribute(.unique) var name: String
              var destination: String
              @Attribute(originalName: "start_date") var startDate: Date
              @Attribute(originalName: "end_date") var endDate: Date
          
              var bucketList: [BucketListItem]? = []
              var livingAccommodation: LivingAccommodation?
          }
      
          // Define the other models in this version...
      }
    • 7:49 - Implementing a SchemaMigrationPlan

      enum SampleTripsMigrationPlan: SchemaMigrationPlan {
          static var schemas: [any VersionedSchema.Type] {
              [SampleTripsSchemaV1.self, SampleTripsSchemaV2.self, SampleTripsSchemaV3.self]
          }
          
          static var stages: [MigrationStage] {
              [migrateV1toV2, migrateV2toV3]
          }
      
          static let migrateV1toV2 = MigrationStage.custom(
              fromVersion: SampleTripsSchemaV1.self,
              toVersion: SampleTripsSchemaV2.self,
              willMigrate: { context in
                  let trips = try? context.fetch(FetchDescriptor<SampleTripsSchemaV1.Trip>())
                            
                  // De-duplicate Trip instances here...
                            
                  try? context.save() 
              }, didMigrate: nil
          )
        
          static let migrateV2toV3 = MigrationStage.lightweight(
              fromVersion: SampleTripsSchemaV2.self,
              toVersion: SampleTripsSchemaV3.self
          )
      }
    • 8:40 - Configuring the migration plan

      struct TripsApp: App {
          let container = ModelContainer(
              for: Trip.self, 
              migrationPlan: SampleTripsMigrationPlan.self
          )
          
          var body: some Scene {
              WindowGroup {
                  ContentView()
              }
              .modelContainer(container)
          }
      }

Developer Footer

  • Vídeos
  • WWDC23
  • Model your schema with SwiftData
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines