Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

Reply to Mac App Crashing with Illegal Instructions
After some digging I found out that SwiftData is causing the problem. More concrete relationships I have a class Account with has (optional) categories @Relationship(deleteRule: .cascade, inverse: Category.account) var _categories: [Category]? var categories: [Category] { self._categories ?? [] } in the class Category I have the a simple account variable var account: Account The application crahses when I use the following init function in Account (at the last line) init(id: Int, income: Bool, location: String, maintainance: Bool, special: Bool, taxRate: Float, tenant: String, title: String) { self.id = id self.income = income self.location = location self.maintainance = maintainance self.special = special self.taxRate = taxRate self.tenant = tenant self.title = title self._categories = [] } or when I use those lines // category not found, so create one let category: Category = Category(title: categoryName, account: self) if self._categories == nil { self._categories = [category] }
Apr ’25
SwiftData Predicate Issue
I'm in the process of converting one of my apps to SwiftData. I converted the other ones over. This app has a much more involved Schema, but SwiftData seems to handle it well. My issue is when I try to fetch data based on a. #Predicate. Here's my code. let predicate = #Predicate { data in data.result == result && data.variable!.starts(with: SomeString) } let sortBy = [SortDescriptor(.result] let descriptor = FetchDescriptor(predicate: predicate, sortBy: sortBy) if let theData = try? context?.fetch(descriptor) { The if let at the bottom fails. Note that 'variable' is optional while result is not. I have to use ! in the predicate since it's a bool that is returned. If my predicate were just data.result == result, then all is well. I can then check the received data for the second condition in the predicate just fine. It just doesn't work inside of the Predicate. The same is true if I used contains. Anybody else having this issue? I'm using Xcode 15.0 beta 6.
3
0
2.1k
Aug ’23
SwiftData not working in VisionOS
I want to make icloud backup using SwiftData in VisionOS and I need to use SwiftData first but I get the following error even though I do the following steps I followed the steps below I created a Model import Foundation import SwiftData @Model class NoteModel { @Attribute(.unique) var id: UUID var date:Date var title:String var text:String init(id: UUID = UUID(), date: Date, title: String, text: String) { self.id = id self.date = date self.title = title self.text = text } } I added modelContainer WindowGroup(content: { NoteView() }) .modelContainer(for: [NoteModel.self]) And I'm making inserts to test import SwiftUI import SwiftData struct NoteView: View { @Environment(.modelContext) private var context var body: some View { Button(action: { // new Note let note = NoteModel(date: Date(), title: New Note, text: ) context.insert(note) }, label: { Image(systemName: note.text.badge.plus) .font(.system(size: 24)) .frame(width: 30, height: 30) .padding(12) .background( RoundedRe
0
0
699
Feb ’24
Reply to initializer override
You should probably look in the init inheritance rules: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.htmlIt says:Rule 2If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.These rules apply even if your subclass adds further convenience initializers.NOTEA subclass can implement a superclass designated initializer as a subclass convenience initializer as part of satisfying rule 2.Your override of 'init(name:)' means that you've overridden all of the superclass's designated initializers. In that case, you inherit the superclass's convenience initializers, just 'init()' in this case, even though you've added further convenience initialzers.
Topic: Programming Languages SubTopic: Swift Tags:
May ’16
SwiftData and CloudKit Issues
Hi, I'm using SwiftData in my app, and I want to sent data to iCloud with CloudKit, but I found that If the user turns off my App iCloud sync function in the settings App, the local data will also be deleted. A better way is maintaining the local data, just don't connect to iCloud.How should I do that? I need guidance!!! I'm just getting started with CloudKit And I would be appreciated!
1
0
265
Nov ’25
SwiftData/ModelContext.swift:3253: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData<Presents_2024.Item> from [:]
I'm still getting this error (SwiftData/ModelContext.swift:3253: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData from [:]) in Xcode 16.1 Beta (16B5001e). The app works for a limited amount of time and then crashes with this error. It looks like the SwiftData model isn't being created properly and when a context is saved it crashes. Can you tell me if this error will be fixed in the next beta?
9
0
2.3k
Sep ’24
Reply to creating archive fails with error «…-Prefix.pch» not found
Seems like I figured it out. The culprit was a totally unrelated build-setting. In Other C Flags my settings differed in debug and release. I had $(inherited) in both of them, but in the release setting I additionally had -isystem - which apparently is already included in $(inherited). That caused a multitude of errors that ended with the prefix.pch not being copied...
May ’21
SwiftData Transformable with String Array
I am doing a full transition of an app from CoreData to SwiftData. I was able to follow the online guides and extract SwiftData objects from the CoreData model. Everything seems to work except for my Transformable String Arrays. This app is storing CoreData in iCloud. In my SwiftData model, I have a variable... @Attribute(.transformable(by: NSSecureUnarchiveFromData)) var arrayofStrings: [String]? The app is able to read the array of strings, however, the debugger gives me this error eventually crashing due to memory. 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release I don't understand, the transformed variable already is 'NSSecureUnarchiveFromData' not 'NSKeyedUnarchiveFromData'. That's the reason why I used NSSecureUnarchiveFromData in my CoreData model because NSKeyedUnarchiveFromData is being phased out. I don't get why the debugger thinks otherwise. Any thoughts?
3
0
2.5k
Jan ’24
Replacing Core Data with SwiftData
I have an app that uses Core Data. I'm switching to SwiftData but it looks like the sqlite files are stored in separate places in the application file directory so my SwiftData files aren't reading the CoreData store. I'm not sure why it's not reading from the same location. Is there something I'm missing? Here's an example of the paths that I see when I write information to the debug console: SwiftData Path: file:///Users/dougthiele/Library/Developer/CoreSimulator/Devices/52CE32F8-F6A9-4825-8027-994DBE47173C/data/Containers/Data/Application/63E9B61D-64B8-4D2D-A02C-3C306688F354/Documents/[Data File Name].sqlite Core Data Path: file:///Users/dougthiele/Library/Developer/CoreSimulator/Devices/52CE32F8-F6A9-4825-8027-994DBE47173C/data/Containers/Data/Application/96A5961B-54DD-43A9-A4C3-661B439D91AE/Documents/[Data File Name].sqlite
1
0
922
Oct ’24
Reply to Cannot invoke 'self()' in beta 2
I'm seeing the same problem with your code in Xcode 7 beta 3.(You need to explictly include .init now, so you would have self.init() but that just gets you the similar error that init can't be invoked without arguments.)It seems that the version of init() inherited from NSObject isn't being recognized when using .init() on a reference to a metatype.This is probably a bug in the Swift compiler, since it seems to be applying the rules for Swift initializer inheritance to a class that has been imported from objC, when accessing it through a dynamic type object (like self in a type method, or a variable holding a metatype).It recognizes the designated initializers inherited from UIView (which could be automatically inherited due to the rules for Swift initializer inheritance), but not designated initializers from ancestors past that point (which would be hidden in a Swift class after UIView declared it's own designated initializers).As a workaround, you might be able t
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’15
Reply to Does SwiftData not allow model class inheritance?
I tried out Swift Data and thought: Surely I'm missing something, because the only way I was able to make inheritance 'work' was by casting to the base/parent class when I wanted to save in the database, and casting to the child class when I wanted to display it, which oddly enough, somehow works, and it somehow saves fields that aren't present on the parent class, but are present on the child class. How or why is beyond me, but yea I'm hoping they add inheritance sometime soon, I'm not even sure why they would release swift data without support for inheritance.
Jul ’23
Reply to Problems with subclassing UICollectionViewLayoutAttributes
Initialization is one of the toughest parts in Swift. Frankly, many aspects of initialization are not crystal clear for me, even now.But, in this case, avaiability of convenience initializer is OK. It's a guranteed feature, not a works-as-for-now thing.Automatic Initializer Inheritance part of the Class Inheritance and Initialization section can be some help.
Topic: UI Frameworks SubTopic: UIKit Tags:
Aug ’15
Reply to Subclassing a class that has convenience initializers.
If you override all of the superclass designated initializers, you'll inherit the superclass convenience initializers. (See Swift docs, Initialization -> Automatic Initializer Inheritance -> Rule 2.) I think in this case that means you can define an init(property1:) that just calls through to super and you should able to use init(value1:value2) for subclass initialization.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’16
Core Data, CloudKit - Deduplication causes nil relationships
I followed along apples Article for relevant store changes, mainly for data deduplication. https://developer.apple.com/documentation/coredata/consuming_relevant_store_changes I also downloaded the Core Data / CloudKit Demo App which already has a deduplication process. https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud In the Demo project I observed that more often than not, Posts loose their relationship to Tags. After some investigation I assume that this happens, when a Tag which has a relationship to a Post, gets deleted during the deduplication process, before the relevant Post was synced to the device. When the Post now arrives on the device, its related Tag Object does no longer exist. Therefore it's also not possible to find the retained, deduped Tag-Object which should be connected to the Post. I'm wondering why this was implemented that way in the Demo Project, as this really causes critical data loss. I have also no Idea how to avoid it. In
1
0
2.1k
Jul ’22
Reply to Mac App Crashing with Illegal Instructions
After some digging I found out that SwiftData is causing the problem. More concrete relationships I have a class Account with has (optional) categories @Relationship(deleteRule: .cascade, inverse: Category.account) var _categories: [Category]? var categories: [Category] { self._categories ?? [] } in the class Category I have the a simple account variable var account: Account The application crahses when I use the following init function in Account (at the last line) init(id: Int, income: Bool, location: String, maintainance: Bool, special: Bool, taxRate: Float, tenant: String, title: String) { self.id = id self.income = income self.location = location self.maintainance = maintainance self.special = special self.taxRate = taxRate self.tenant = tenant self.title = title self._categories = [] } or when I use those lines // category not found, so create one let category: Category = Category(title: categoryName, account: self) if self._categories == nil { self._categories = [category] }
Replies
Boosts
Views
Activity
Apr ’25
SwiftData Predicate Issue
I'm in the process of converting one of my apps to SwiftData. I converted the other ones over. This app has a much more involved Schema, but SwiftData seems to handle it well. My issue is when I try to fetch data based on a. #Predicate. Here's my code. let predicate = #Predicate { data in data.result == result && data.variable!.starts(with: SomeString) } let sortBy = [SortDescriptor(.result] let descriptor = FetchDescriptor(predicate: predicate, sortBy: sortBy) if let theData = try? context?.fetch(descriptor) { The if let at the bottom fails. Note that 'variable' is optional while result is not. I have to use ! in the predicate since it's a bool that is returned. If my predicate were just data.result == result, then all is well. I can then check the received data for the second condition in the predicate just fine. It just doesn't work inside of the Predicate. The same is true if I used contains. Anybody else having this issue? I'm using Xcode 15.0 beta 6.
Replies
3
Boosts
0
Views
2.1k
Activity
Aug ’23
SwiftData not working in VisionOS
I want to make icloud backup using SwiftData in VisionOS and I need to use SwiftData first but I get the following error even though I do the following steps I followed the steps below I created a Model import Foundation import SwiftData @Model class NoteModel { @Attribute(.unique) var id: UUID var date:Date var title:String var text:String init(id: UUID = UUID(), date: Date, title: String, text: String) { self.id = id self.date = date self.title = title self.text = text } } I added modelContainer WindowGroup(content: { NoteView() }) .modelContainer(for: [NoteModel.self]) And I'm making inserts to test import SwiftUI import SwiftData struct NoteView: View { @Environment(.modelContext) private var context var body: some View { Button(action: { // new Note let note = NoteModel(date: Date(), title: New Note, text: ) context.insert(note) }, label: { Image(systemName: note.text.badge.plus) .font(.system(size: 24)) .frame(width: 30, height: 30) .padding(12) .background( RoundedRe
Replies
0
Boosts
0
Views
699
Activity
Feb ’24
Reply to initializer override
You should probably look in the init inheritance rules: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.htmlIt says:Rule 2If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.These rules apply even if your subclass adds further convenience initializers.NOTEA subclass can implement a superclass designated initializer as a subclass convenience initializer as part of satisfying rule 2.Your override of 'init(name:)' means that you've overridden all of the superclass's designated initializers. In that case, you inherit the superclass's convenience initializers, just 'init()' in this case, even though you've added further convenience initialzers.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’16
SwiftData and CloudKit learning resource
Perhaps I should not be asking this here, but can anyone recommend a good learning resource/tutorial for a complete beginner for SwiftData and CloudKit?
Replies
2
Boosts
0
Views
660
Activity
Jun ’24
SwiftData and CloudKit Issues
Hi, I'm using SwiftData in my app, and I want to sent data to iCloud with CloudKit, but I found that If the user turns off my App iCloud sync function in the settings App, the local data will also be deleted. A better way is maintaining the local data, just don't connect to iCloud.How should I do that? I need guidance!!! I'm just getting started with CloudKit And I would be appreciated!
Replies
1
Boosts
0
Views
265
Activity
Nov ’25
SwiftData/ModelContext.swift:3253: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData<Presents_2024.Item> from [:]
I'm still getting this error (SwiftData/ModelContext.swift:3253: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData from [:]) in Xcode 16.1 Beta (16B5001e). The app works for a limited amount of time and then crashes with this error. It looks like the SwiftData model isn't being created properly and when a context is saved it crashes. Can you tell me if this error will be fixed in the next beta?
Replies
9
Boosts
0
Views
2.3k
Activity
Sep ’24
Reply to creating archive fails with error «…-Prefix.pch» not found
Seems like I figured it out. The culprit was a totally unrelated build-setting. In Other C Flags my settings differed in debug and release. I had $(inherited) in both of them, but in the release setting I additionally had -isystem - which apparently is already included in $(inherited). That caused a multitude of errors that ended with the prefix.pch not being copied...
Replies
Boosts
Views
Activity
May ’21
SwiftData Transformable with String Array
I am doing a full transition of an app from CoreData to SwiftData. I was able to follow the online guides and extract SwiftData objects from the CoreData model. Everything seems to work except for my Transformable String Arrays. This app is storing CoreData in iCloud. In my SwiftData model, I have a variable... @Attribute(.transformable(by: NSSecureUnarchiveFromData)) var arrayofStrings: [String]? The app is able to read the array of strings, however, the debugger gives me this error eventually crashing due to memory. 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release I don't understand, the transformed variable already is 'NSSecureUnarchiveFromData' not 'NSKeyedUnarchiveFromData'. That's the reason why I used NSSecureUnarchiveFromData in my CoreData model because NSKeyedUnarchiveFromData is being phased out. I don't get why the debugger thinks otherwise. Any thoughts?
Replies
3
Boosts
0
Views
2.5k
Activity
Jan ’24
Replacing Core Data with SwiftData
I have an app that uses Core Data. I'm switching to SwiftData but it looks like the sqlite files are stored in separate places in the application file directory so my SwiftData files aren't reading the CoreData store. I'm not sure why it's not reading from the same location. Is there something I'm missing? Here's an example of the paths that I see when I write information to the debug console: SwiftData Path: file:///Users/dougthiele/Library/Developer/CoreSimulator/Devices/52CE32F8-F6A9-4825-8027-994DBE47173C/data/Containers/Data/Application/63E9B61D-64B8-4D2D-A02C-3C306688F354/Documents/[Data File Name].sqlite Core Data Path: file:///Users/dougthiele/Library/Developer/CoreSimulator/Devices/52CE32F8-F6A9-4825-8027-994DBE47173C/data/Containers/Data/Application/96A5961B-54DD-43A9-A4C3-661B439D91AE/Documents/[Data File Name].sqlite
Replies
1
Boosts
0
Views
922
Activity
Oct ’24
Reply to Cannot invoke 'self()' in beta 2
I'm seeing the same problem with your code in Xcode 7 beta 3.(You need to explictly include .init now, so you would have self.init() but that just gets you the similar error that init can't be invoked without arguments.)It seems that the version of init() inherited from NSObject isn't being recognized when using .init() on a reference to a metatype.This is probably a bug in the Swift compiler, since it seems to be applying the rules for Swift initializer inheritance to a class that has been imported from objC, when accessing it through a dynamic type object (like self in a type method, or a variable holding a metatype).It recognizes the designated initializers inherited from UIView (which could be automatically inherited due to the rules for Swift initializer inheritance), but not designated initializers from ancestors past that point (which would be hidden in a Swift class after UIView declared it's own designated initializers).As a workaround, you might be able t
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’15
Reply to Does SwiftData not allow model class inheritance?
I tried out Swift Data and thought: Surely I'm missing something, because the only way I was able to make inheritance 'work' was by casting to the base/parent class when I wanted to save in the database, and casting to the child class when I wanted to display it, which oddly enough, somehow works, and it somehow saves fields that aren't present on the parent class, but are present on the child class. How or why is beyond me, but yea I'm hoping they add inheritance sometime soon, I'm not even sure why they would release swift data without support for inheritance.
Replies
Boosts
Views
Activity
Jul ’23
Reply to Problems with subclassing UICollectionViewLayoutAttributes
Initialization is one of the toughest parts in Swift. Frankly, many aspects of initialization are not crystal clear for me, even now.But, in this case, avaiability of convenience initializer is OK. It's a guranteed feature, not a works-as-for-now thing.Automatic Initializer Inheritance part of the Class Inheritance and Initialization section can be some help.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Aug ’15
Reply to Subclassing a class that has convenience initializers.
If you override all of the superclass designated initializers, you'll inherit the superclass convenience initializers. (See Swift docs, Initialization -> Automatic Initializer Inheritance -> Rule 2.) I think in this case that means you can define an init(property1:) that just calls through to super and you should able to use init(value1:value2) for subclass initialization.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jan ’16
Core Data, CloudKit - Deduplication causes nil relationships
I followed along apples Article for relevant store changes, mainly for data deduplication. https://developer.apple.com/documentation/coredata/consuming_relevant_store_changes I also downloaded the Core Data / CloudKit Demo App which already has a deduplication process. https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud In the Demo project I observed that more often than not, Posts loose their relationship to Tags. After some investigation I assume that this happens, when a Tag which has a relationship to a Post, gets deleted during the deduplication process, before the relevant Post was synced to the device. When the Post now arrives on the device, its related Tag Object does no longer exist. Therefore it's also not possible to find the retained, deduped Tag-Object which should be connected to the Post. I'm wondering why this was implemented that way in the Demo Project, as this really causes critical data loss. I have also no Idea how to avoid it. In
Replies
1
Boosts
0
Views
2.1k
Activity
Jul ’22