Hi! I'm building an app from production Xcode_15.4.0 and I'm seeing strange behavior from the Model
macro:
import SwiftData
@Model package class Person {
init() {
}
}
Building this from Xcode_15.4.0 or Swift 5.10 leads to these errors:
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMe_.swift:1:1: error: initializer 'init(backingData:)' must be as accessible as its enclosing type because it matches a requirement in protocol 'PersistentModel'
extension Person: SwiftData.PersistentModel {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMm_.swift:19:10: note: mark the initializer as 'package' to satisfy the requirement
required init(backingData: any SwiftData.BackingData<Person>) {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMe_.swift:1:1: error: property 'schemaMetadata' must be as accessible as its enclosing type because it matches a requirement in protocol 'PersistentModel'
extension Person: SwiftData.PersistentModel {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMm_.swift:13:12: note: mark the static property as 'package' to satisfy the requirement
static var schemaMetadata: [SwiftData.Schema.PropertyMetadata] {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMe_.swift:1:1: error: initializer 'init(backingData:)' must be as accessible as its enclosing type because it matches a requirement in protocol 'PersistentModel'
extension Person: SwiftData.PersistentModel {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMm_.swift:19:10: note: mark the initializer as 'package' to satisfy the requirement
required init(backingData: any SwiftData.BackingData<Person>) {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMe_.swift:1:1: error: property 'schemaMetadata' must be as accessible as its enclosing type because it matches a requirement in protocol 'PersistentModel'
extension Person: SwiftData.PersistentModel {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/folders/1j/0r1s_v0n4bn200kt9nkm9j5w0000gn/T/swift-generated-sources/@__swiftmacro_9MyLibrary6Person5ModelfMm_.swift:13:12: note: mark the static property as 'package' to satisfy the requirement
static var schemaMetadata: [SwiftData.Schema.PropertyMetadata] {
^
/Users/rick/Desktop/MyLibrary/Sources/MyLibrary/MyLibrary.swift:3:1: note: in expansion of macro 'Model' on class 'Person' here
@Model package class Person {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: fatalError
Building from Xcode_16_beta_4 or Swift 6.0 builds with no errors.
Is this package
issue being tracked for SwiftData when building from 5.10? It looks like this is fixed from 6.0… but I would like to build this code from production Swift today.
Potential workarounds:
- Mark the class as
internal
orpublic
? - Use Xcode to inline the macro expansion and directly modify the broken functions with the correct access control?
- Any more ideas?
My preference would be to keep this type package
(while also building from 5.10). Any more workarounds (other than expanding the macro and modifying the functions myself by-hand)? Thanks!