I'm building an application with SwiftUI and SwiftData. Up until a couple days ago, everything was working fine. Then, Xcode auto-updated to v16 in the background, and the next time I opened Xcode and tried to build my app it wouldn't build anymore, citing some errors in expanding the SwiftData @Model
macro on one of my objects. Attached are the errors specifically, shown where Xcode shows them (in the expanded @Model
macro). In text, they are:
- Instance method 'access(_:keyPath:)' requires that 'Member' conform to 'Observable'
- Cannot convert value of type 'Risers.Member' to expected argument type 'Member'
- Instance method 'withMutation(of:keyPath:_:)' requires that 'Member' conform to 'Observable'
- Cannot convert value of type 'Risers.Member' to expected argument type 'Member'
Here is the SwiftData class in full:
import SwiftData
import SwiftUI
@Model
class Member: Identifiable, Hashable {
var chorus: Chorus?
var id = UUID()
var firstName: String
var lastName: String
var fullName: String {
"\(firstName) \(lastName)"
}
var voicePart: Int
var voicePartString: String? {
chorus?.voicePartType.prettyName(forPart: voicePart)
}
@Attribute(.externalStorage) var pictureData: Data
init(chorus: Chorus? = nil,
firstName: String = "",
lastName: String = "",
voicePart: Int = 1,
pictureData: Data = Data()) {
self.chorus = chorus
self.firstName = firstName
self.lastName = lastName
self.voicePart = voicePart
self.pictureData = pictureData
}
init(member: Member) {
self.chorus = member.chorus
self.firstName = member.firstName
self.lastName = member.lastName
self.voicePart = member.voicePart
self.pictureData = member.pictureData
}
I tried building again on Xcode 15.4, and it still builds successfully there. Xcode 16.1 beta has not made a difference. Is this my fault, or is Xcode 16 broken?
You can provide access(_:keyPath:)
and withMutation(of:keyPath:_:)
with a fully qualified name for your class Member
in order to avoid the compilation issue:
internal nonisolated func access<Member>(
keyPath: KeyPath<YOURAPPDOMAINNAME.Member, Member>
) {
_$observationRegistrar.access(self, keyPath: keyPath)
}
internal nonisolated func withMutation<Member, MutationResult>(
keyPath: KeyPath<YOURAPPDOMAINNAME.Member, Member>,
_ mutation: () throws -> MutationResult
) rethrows -> MutationResult {
try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}