Question:
Hello,
I'm encountering an issue with SwiftData in Xcode. Despite setting up my model classes correctly, Xcode is suggesting additional boilerplate code for handling backing data which I believe should not be necessary. Here are the details:
Context:
I'm working with SwiftData to persist my data models. I've set up my model container and schema correctly, and I'm only persisting final classes. However, Xcode is suggesting the following code for one of my model classes (LoanAccount):
swift
Copy code
@Transient
private var _$backingData: any SwiftData.BackingData = LoanAccount.createBackingData()
public var persistentBackingData: any SwiftData.BackingData {
get {
_$backingData
}
set {
_$backingData = newValue
}
}
static var schemaMetadata: [SwiftData.Schema.PropertyMetadata] {
return [
SwiftData.Schema.PropertyMetadata(name: "loanName", keypath: \LoanAccount.loanName, defaultValue: nil, metadata: nil),
SwiftData.Schema.PropertyMetadata(name: "outstandingBalance", keypath: \LoanAccount.outstandingBalance, defaultValue: nil, metadata: nil),
SwiftData.Schema.PropertyMetadata(name: "currentAssetValue", keypath: \LoanAccount.currentAssetValue, defaultValue: nil, metadata: nil),
SwiftData.Schema.PropertyMetadata(name: "securedAssets", keypath: \LoanAccount.securedAssets, defaultValue: [], metadata: nil)
]
}
required init(backingData: any SwiftData.BackingData) {
_loanName = _SwiftDataNoType()
_outstandingBalance = _SwiftDataNoType()
_currentAssetValue = _SwiftDataNoType()
_securedAssets = _SwiftDataNoType()
self.persistentBackingData = backingData
}
@Transient
private let _$observationRegistrar = Observation.ObservationRegistrar()
struct _SwiftDataNoType {
}
My Model Setup:
Here's a brief overview of my model setup:
swift
Copy code
import Foundation
import SwiftData
@Model
class LoanAccount: LiabilityAccount {
var loanName: String
var outstandingBalance: Double?
var currentAssetValue: Double?
var securedAssets: [SecuredAsset] = []
required init(
id: UUID = UUID(),
institutionName: String,
accountName: String,
accountBalance: Double = 0,
accountOwner: String,
country: String = "UK",
accountCurrency: String,
risk: Int = 1,
accountStatus: String = "Active",
startDate: Date = Date(),
maturityDate: Date = Date(),
dateCreated: Date = Date(),
dateUpdated: Date = Date(),
addressline1: String? = nil,
addressline2: String? = nil,
county: String? = nil,
zipcode: String? = nil,
phoneNumber: String? = nil,
email: String? = nil,
contact1: String? = nil,
contact2: String? = nil,
link: String? = nil,
notes: String? = nil,
accountNumber: String? = nil,
sortCode: String? = nil,
accountFee: Double? = nil,
interestRate: Double? = nil,
loanName: String,
outstandingBalance: Double? = nil,
currentAssetValue: Double? = nil,
securedAssets: [SecuredAsset] = [],
activities: [AccountActivity] = []
) {
self.loanName = loanName
self.outstandingBalance = outstandingBalance
self.currentAssetValue = currentAssetValue
self.securedAssets = securedAssets
super.init(
id: id,
institutionName: institutionName,
accountName: accountName,
accountType: "Loan",
icon: "default_icon",
accountOwner: accountOwner,
country: country,
accountCurrency: accountCurrency,
risk: risk,
accountStatus: accountStatus,
startDate: startDate,
maturityDate: maturityDate,
dateCreated: dateCreated,
dateUpdated: dateUpdated,
addressline1: addressline1,
addressline2: addressline2,
county: county,
zipcode: zipcode,
phoneNumber: phoneNumber,
email: email,
contact1: contact1,
contact2: contact2,
link: link,
notes: notes,
accountNumber: accountNumber,
sortCode: sortCode,
accountFee: accountFee,
interestRate: interestRate,
accountBalance: accountBalance,
activities: activities
)
}
}
Issue:
Xcode is suggesting that I need to add the @Transient backing data code, even though my understanding is that this should be handled automatically by SwiftData when using the @Model attribute.
Request:
Can anyone provide insight into why Xcode is suggesting this code and if there's a configuration or setup step I might be missing? I want to ensure my data models are set up correctly without needing unnecessary boilerplate code.
Thank you!