Can't set default values for @Model vars

If I have an @Model object and I try to add a default/initial value to it, my code no longer compiles:

var createdAt: Date // compiles

var createdAt: Date = Date.now // fails

it gives me this error:

Variable 'self._$backingData' used before being initialized

this worked fine in Xcode b5, but is failing in b6

The same question

I believe they want us to set default values inside the initializer of the model and because

@Model requires an initializer be provided for 'MyModel'

I think it is fine

+1, SwiftData Sync via Cloud needs default values, so this doesn't work in Xcode 15 beta 6

I have the same issue. Working fine on xCode B5, but now begins to fail with aforementioned error on B6.

I see the same issue and already submitted a feedback: FB12905731

Same issue here

I think this relates to this issue mentioned in the release notes of Xcode Beta 6:

@Model classes with initial values for stored properties result in an error that self._$backingData is used before being initialized. (113572344) Workaround: Assign initial values to stored properties in the body of an initializer. For example, instead of this code: [...]

Same problem here, can not compile with initial values, but if not set, can not open the container at runtime. I used to avoid this problem in Core Data by making all properties optional and providing non-optional calculated properties for ease of use.

I'm on Xcode 15 beta 6 and simply removing the default values caused the model container to crash.

Making the properties optional worked for me.

According to the Xcode 15 Beta 7 Release Notes:

SwiftData

Resolved Issues

  • Fixed: @Model classes with initial values for stored properties result in an error that self._$backingData is used before being initialized. (113572344)

New builds with the new Xcode beta should not run into those issues.

I think, its all because SwiftData is only can be used in iOS 17+, so if you project not, you get this error

Can't set default values for @Model vars
 
 
Q