use of @Model macro from SwiftData causes project build to fail

Target

tvOS 17.4

macOS Version

14.5

Xcode Version

15.4 (15F31d)

I am working on integrating SwiftData (not migrating from CoreData) into an app in active development for tvOS, and I cannot get the project to compile whenever the @Model macro is introduced.

I've traced every error back to the code generated by the @Model, and commenting it out removes the errors. I am able to use @Model on a fresh project, but any attempt to import SwiftData results in the build failing with the following errors:

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModel0D0fMe_.swift:1:1 Type 'DeviceModel' does not conform to protocol 'Observable'

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModelC2id18_PersistedPropertyfMa_.swift:4:9 Member 'setValue' cannot be used on value of type 'any BackingData<DeviceModel>'; consider using a generic constraint instead

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModel0D0fMe_.swift:4:36 'Observable' is not a member type of struct 'AeolusKit.Observation'

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModelC2id18_PersistedPropertyfMa_.swift:4:50 Cannot convert value of type 'String' to expected argument type 'PersistentIdentifier'

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModelC2id18_PersistedPropertyfMa_.swift:8:54 Cannot infer key path type from context; consider explicitly specifying a root type

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModelC2id18_PersistedPropertyfMa_.swift:12:64 Cannot infer key path type from context; consider explicitly specifying a root type

/var/folders/t5/c9qp7_4j19x9s3p3z26kn6v80000gn/T/swift-generated-sources/@__swiftmacro_9AeolusKit11DeviceModel0D0fMm_.swift:25:50 Type 'Observation' has no member 'ObservationRegistrar'

the code in question causing the error:

import Foundation
import SwiftData

@Model
public final class DeviceModel {
    @Attribute(.unique) public var id: String
    
    init(id: String) {
        self.id = id
    }
}

I've already done the following:

  • clean the project
  • erase the contents of the derived directory
  • restart xcode
  • restart my mac.

I want to emphasize, I do not believe it is my code causing this issue, as commenting out the @Model result's in a perfectly normal build with no warnings or errors

Any help is appreciated

Answered by theta71 in 789919022

@DTS Engineer The issue stemmed from a naming conflict. I had named a class in my source code "Observation" which resulted in the build errors.

Do you define, or use a 3rd-party library that defines, a type named Observable, which conflicts with the system-provided Observable protocol? Based on the error message, it seems that Xcode got confused about Observable.

If that isn't your case, you can probably consider providing a reproducible case for folks to take a closer look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

@DTS Engineer The issue stemmed from a naming conflict. I had named a class in my source code "Observation" which resulted in the build errors.

use of @Model macro from SwiftData causes project build to fail
 
 
Q