Questions tagged [swift-data]

Swift Data is a data modeling framework from Apple for use in apps running on their operating systems, including iOS, macOS, and others. It's closely related to Core Data but is used in different, more Swift-friendly ways.

92 questions
12
votes
3 answers

How to persist custom enum with SwiftData?

I want to save a custom enum to my SwiftData model. I can get it to work that it shows and even updates the value in the UI. Though, the change of the enum var/entity isn’t persisted when relaunching the app! Any advice what to watch out for when…
alexkaessner
  • 1,966
  • 1
  • 14
  • 39
11
votes
7 answers

How to resolve SwiftData error "Type '*' does not conform to protocol 'PersistentModel'"

Working through the betas of SwiftData and trying to stand up a PersistentContainer. I set up a modelContainer View modifier on my ContentView and conformed my class to @Model as described in various WWDC videos (the ones that are out). Here is the…
Brianna Doubt
  • 464
  • 2
  • 11
8
votes
3 answers

SwiftData list bindings with @Query

Since Swift 5.5 we could create SwiftUI lists with bindings like this (e.g. see this answer): class Item { // ... var isOn: Bool } struct ContentView: View { @State private var items: [Item] = [] var body: some View { …
de.
  • 7,068
  • 3
  • 40
  • 69
6
votes
1 answer

SwiftData passing @Model objects as parameters in SwiftUI

I am trying to work with multiple views in a SwiftData based SwiftUI project. However, when I create a parameter for a SwiftData @Model object in another view, when I create a sample of that object in the #Preview, the compiler starts complaining…
Syd Polk
  • 73
  • 1
  • 5
6
votes
0 answers

SwiftData - How to observe changes to the database outside of SwiftUI?

I have a simple example. I fetch all Trip objects from my database: import SwiftData @Model class Trip { var name: String } func fetch() { let container = ModelContainer(for: Trip.self) let context = ModelContext(container) let…
SwiftedMind
  • 3,701
  • 3
  • 29
  • 63
6
votes
2 answers

SwiftData: Use ObjectID URIRepresentation to load CoreData object?

I am replacing some CoreData code within my iOS App to use the new SwiftData framework. In my original code, I was saving a CoreData object URI in UserDefaults for a quick bookmark. This allowed me to easily fetch specific object I needed…
DookieMan
  • 992
  • 3
  • 10
  • 26
5
votes
0 answers

SwiftData IOS 17 Array in random order?

Why is the order of my array random when i use the @Model macro. class TestModel { var name: String? var array: \[TestModel2\] init(name: String = "") { self.name = name array = [] } } class TestModel2 { var name:…
5
votes
1 answer

How can we use custom structs/enums with the Model and Predicate macros?

Swift 5.9 and the new SwiftData framework introduce the @Model and #Predicate macros. We can now use custom enums and structs in our our models, like so: @Model final class Item { var name: Name var nature: Nature struct Name: Codable…
parapote
  • 433
  • 2
  • 8
3
votes
1 answer

Receiving CoreData error messages when using SwiftData in Xcode 15 beta 5

I am using SwiftData in my project. All worked fine under Xcode 15 beta 4 in the simulator. Since I changed to Xcode 15 beta 5 I am getting the following error multiple times: CoreData: fault: One or more models in this application are…
Mike
  • 51
  • 5
3
votes
1 answer

SwiftData Modeling and saving non Nullable Relations

I have the following code: import SwiftUI import SwiftData struct ContentView: View { @Environment(\.modelContext) private var modelContext var schoolClass: SchoolClass var body: some View { VStack { …
David Schilling
  • 2,442
  • 3
  • 17
  • 24
3
votes
2 answers

SwiftData query with dynamic properties in a View

I'm trying to figure out how to make a SwiftUI view that displays data from SwiftData using a query that includes variables passed into the view. I'm guessing that I won't be able to use the @Query syntax, but has anyone come up with a workable…
Mike Bedar
  • 632
  • 5
  • 14
2
votes
0 answers

SwiftData inverse Relationship stop working in XCode 15 beta 7

This approach works before XCode 15 beta 7: @Model final class Item { var name: String @Relationship(inverse:\Note.item) var notes: [Note] init(name: String = "Item name") { self.name = name self.notes = [] } } @Model…
2
votes
1 answer

SwiftData, Xcode 15 beta 6: Variable 'self._$backingData' used before being initialized

This code worked perfectly fine with Xcode 15 beta 2. @Model final class Room { var name: String = "" @Relationship var plants: [Plant]? init(name: String, plants: [Plant] = []) { self.name = name } } Right now, with…
Jakub Gawecki
  • 801
  • 2
  • 6
  • 14
2
votes
0 answers

SwiftData - 'self' used in property access 'persistentBackingData' before 'super.init' call

I was trying to set a class to be a persistent data source using @Model annotation, and somehow this happened: The code above is generated by the compiler and the error is located in it. Here is my code: import SwiftData class Animal…
2
votes
2 answers

Crash when accessing relationship property of SwiftData model

I have two one-to-many models and I'm getting an unexplained crash when trying to access the relationship properties of the models. The reason for the error is as follows: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1038f4448) Xcode shows that the…
ikunhub
  • 73
  • 1
  • 7
1
2 3 4 5 6 7