Decodable Conformance on PersistentModels in Swift Data

Hello,

I have just tried playing around with the new Swift Data framework and noticed something.

Assume the following many-to-many relationship on two PersistentModels:

@Model
class Media {
    @Relationship(.nullify, inverse: \Tag.medias)
    var tags: [Tag]
}

@Model
class Tag {
    var medias: [Media]
}

This compiles without problems. If we now make Media conform to Codable (or just Decodable), we get a compiler error: "Ambiguous use of 'getValue(for:)'".

When expanding the @Model and the then revealed @PersistedProperty macro, we see that the error is in the getter of Tag.medias, where we call self.getValue(for: \.medias). It seems the compiler knows multiple overloads of this function, including:

Since Media conforms to both protocols, the compiler understandably does not know which overload to use.

So to my questions:

  1. Is this intended behavior? So are PersistentModels not supposed to be Decodable?
  2. If yes, what would be the preferred way (or a clean way) to decode a PersistentModel (e.g., from an API)?

Best regards, Jonas

Post not yet marked as solved Up vote post of Jonas Frey Down vote post of Jonas Frey
1.1k views
  • Same problema here. Compiler errors: Ambiguous use of 'getValue(for:)' and Ambiguous use of 'setValue(for:)'

Add a Comment

Replies

Same problem here. Any news on this?

Still not fixed in xcode 15 Beta 3 🥲

Post not yet marked as solved Up vote reply of JFER Down vote reply of JFER

I hope Beta 4 resolves this. Another 2 week wait.

I am on Xcode 15 beta 4 with no fix in sight.

I hope that this is not intended and is just a bug? It would be nice to have my models also conform to Codable.