Post

Replies

Boosts

Views

Activity

Reply to Does SwiftData not allow model class inheritance?
Any progress on this issue please? I have a base class and two subclass levels like this @Model class CreditCardAccount: LiabilityAccount { } class LiabilityAccount: Account, LiabilityAccountProtocol { class Account: Identifiable, AccountProtocol, Codable { I have multiple classes similar to "CreditCardAccount": and would only instantiate the class at this level so I only need to add @Model to subclasses I want to persist. Hence "Account" and "LiabailityAccount" are not persisted. Xcode throws up the same suggested code to a ll my subclasses which is the I should add this "@Transient private var _$backingData: any SwiftData.BackingData = CreditCardAccount.createBackingData() public var persistentBackingData: any SwiftData.BackingData { get { _$backingData } set { _$backingData = newValue } } static var schemaMetadata: [SwiftData.Schema.PropertyMetadata] { return [ SwiftData.Schema.PropertyMetadata(name: "creditLimit", keypath: \CreditCardAccount.creditLimit, defaultValue: nil, metadata: nil) ] } required init(backingData: any SwiftData.BackingData) { _creditLimit = _SwiftDataNoType() self.persistentBackingData = backingData } @Transient private let _$observationRegistrar = Observation.ObservationRegistrar() struct _SwiftDataNoType { }"
Jun ’24
Reply to Accessing array field with SwiftData throws exception
Seems to be a bug, this code gives me a similar compiler error import Foundation import SwiftUI import SwiftData // DataTopModelClass.swift // Oracle AI // // Created by Steven Ebrey on 29/08/2023. // @Model final class TopModel: ObservableObject { var id: UUID var currentExpertise: Expertise var previousExpertise: [Expertise] init(id: UUID, currentExpertise: Expertise) { self.id = id self.currentExpertise = currentExpertise self.previousExpertise = [] } } extension TopModel: Identifiable {} extension TopModel: Equatable { static func == (lhs: TopModel, rhs: TopModel) -> Bool { return lhs.id == rhs.id && lhs.currentExpertise == rhs.currentExpertise && lhs.previousExpertise == rhs.previousExpertise } } extension TopModel: Hashable { func hash(into hasher: inout Hasher) { hasher.combine(id) hasher.combine(currentExpertise) hasher.combine(previousExpertise) // hasher.combine(messagesmodel) } } Here is compiler error { @storageRestrictions(accesses: _$backingData, initializes: _currentExpertise) init(initialValue) { _$backingData.setValue(forKey: .currentExpertise, to: initialValue) _currentExpertise = _SwiftDataNoType() } get { _$observationRegistrar.access(self, keyPath: \.currentExpertise) return self.getValue(forKey: \.currentExpertise) } set { _$observationRegistrar.withMutation(of: self, keyPath: \.currentExpertise) { self.setValue(forKey: \.currentExpertise, to: newValue) } } }
Sep ’23