After adding a property of a custom struct type, a SwiftData @Model class fails with "no exact matches in call to instance method 'getValue'"; why, and what fixes it?
answer
- read the macro expansion
- generated getter and setter
- constrained overloads
- value, model or collection
basics
~20 s@Model rewrites each stored property into accessors that call getValue(forKey:) and setValue(forKey:to:), whose overloads need a Codable value, a model or a model collection. A plain struct matches none; make it Codable, or use .transformable(by:) or @Transient.
solid answer
~40 sThe error comes from code the macro wrote, not from your line. `@Model` attaches a property macro to every stored property, and the expansion's getter calls `self.getValue(forKey: \.reminder)` while the setter and the `init` accessor call `setValue(forKey:to:)`. SwiftData declares those methods only for constrained types: `Decodable`/`Encodable` values, a `PersistentModel`, or a relationship collection of models, plus transformable variants. A plain struct satisfies none, so overload resolution fails three times. The compiler notes list only the `PersistentModel` and `RelationshipCollection` candidates, which tempts people to make the struct a model; the usual fix is to conform it to `Codable`, after which SwiftData stores it as a composite value of the model. If you cannot make the type `Codable`, use `@Attribute(.transformable(by:))`; if it should not be saved at all, mark it `@Transient`.
code
plaintext · 3 linesmacro expansion @_PersistedProperty:9:21: error: no exact matches in call to instance method 'getValue'
note: candidate requires that 'Reminder' conform to 'PersistentModel' (requirement specified as 'Value' : 'PersistentModel')
note: candidate requires that 'Reminder' conform to 'RelationshipCollection' (requirement specified as 'Value' : 'RelationshipCollection')go deeper
Remember that custom structs and enums stored in a model must be Codable.
Explain that the macro rewrites properties into getValue and setValue calls constrained to storable kinds of value.
Read a macro-expansion diagnostic without being misled by its candidate notes, and choose between Codable, transformable, transient and a separate model.
Agree on which value types may live inside models, since every stored struct becomes part of the schema that later versions must migrate.
## Where the error comes from SwiftData's `@Model` macro does not store properties by magic: it rewrites them. Each persisted stored property receives a generated property macro whose expansion, for `var reminder: Reminder`, looks like this in the iOS 27 SDK (the accessor block the compiler prints when it dumps the expansion): ```plaintext { @storageRestrictions(accesses: _$backingData, initializes: _reminder) init(initialValue) { _$backingData.setValue(forKey: \.reminder, to: initialValue) _reminder = _SwiftDataNoType() } get { _$observationRegistrar.access(self, keyPath: \.reminder) return self.getValue(forKey: \.reminder) } set { _$observationRegistrar.withMutation(of: self, keyPath: \.reminder) { self.setValue(forKey: \.reminder, to: newValue) } } } ``` So the failing calls are in the macro's code, which is why Xcode reports them inside a "macro expansion" block attached to your property. Expanding the macro in the editor shows the same text. ## Which overloads exist `PersistentModel` declares a family of `getValue(forKey:)` and `setValue(forKey:to:)` overloads, each constrained to a kind of value SwiftData knows how to store: - **Codable values**: a getter requiring `Decodable` and a setter requiring `Encodable`. These cover primitives and your own `Codable` structs and enums. The SwiftData interface marks them as disfavored overloads. - **A single related model**: `Value: PersistentModel`, plus an optional variant. - **A collection of models**: `Value: RelationshipCollection`. - **Transformable values**: `getTransformableValue(forKey:)` and `setTransformableValue(forKey:to:)`, which the macro uses for a property marked `.transformable(by:)`. A struct that conforms to none of these leaves every call without a match. The diagnostic notes list only the `PersistentModel` and `RelationshipCollection` candidates, not the `Codable` ones, which is misleading: making the struct a model is impossible, because `PersistentModel` is class-only. ## The fixes, in order of preference 1. **Conform the type to `Codable`.** The struct's own stored properties must be `Codable` too. SwiftData then stores the value as a **composite attribute**: inspecting `Schema([Habit.self])` shows `reminder` as a `Schema.CompositeAttribute` whose own properties are `hour` and `minute`. The value is copied into each owner and has no identity of its own. 2. **Use `@Attribute(.transformable(by:))`** when the type cannot be `Codable`, typically a class such as `UIColor`. The property then goes through the transformable accessors and a registered `ValueTransformer`. 3. **Mark it `@Transient`** if it was never meant to be saved; the macro then leaves it alone, though it needs a default value. 4. **Make it a `@Model` class** only if it needs its own identity, such as being shared or fetched independently. That turns it into a relationship, covered in the lesson on SwiftData relationships. ## Related macro errors | What you wrote | What the 6.4 compiler says | Fix | |---|---|---| | Property of a non-Codable struct | no exact matches in call to instance method 'getValue' / 'setValue' | Make the type `Codable` | | `let createdAt: Date` | cannot expand accessor macro on variable declared with 'let' | Use `var`, with `private(set)` if needed | | `@Transient var count: Int` with no default | @Transient requires non-optional property 'count' to have a default value | Add a default or make it optional | | `@ObservationIgnored` on a stored property | expansion of macro 'ObservationIgnored()' produced an unexpected getter | Use `@Transient` | The `let` case is a warning in the Swift 5 language mode and an error in Swift 6 mode. ## What composite storage means Once the struct is `Codable`, it is a value of the model, not a thing with its own identity. That has practical consequences: - Two habits with equal reminders hold two independent copies; changing one does not touch the other. - Replacing a single field, `habit.reminder?.hour = 7`, goes through the generated setter, so the change is saved and observed like any other property change. - `Codable` enums behave the same way; a `String`-backed enum also appeared as a composite attribute in the test schema. - Adding, removing or renaming a field of the struct changes the stored schema, so treat such edits as schema changes, which the lesson on migrations covers. ## How to read these errors quickly - Look for the **macro expansion** block in the diagnostic and the generated method it names. - Read the **candidate notes** as a list of what SwiftData can store, not as advice. - Check the **type of the property**, then that type's own stored properties, until you find the one that is not storable.
- Why does a `let` property in a SwiftData @Model class produce a different macro error?`@Model` attaches an accessor macro to each stored property, and Swift cannot add accessors to a `let`. The 6.4 compiler reports "cannot expand accessor macro on variable declared with 'let'", a warning in the Swift 5 language mode and an error in Swift 6 mode. Declare it `var`, with `private(set)` if callers must not write it.
- When should the custom type become a @Model class rather than a Codable struct?When it needs its own identity: shared by several owners, fetched on its own, or edited independently. A `Codable` value is copied into each owning model and has no identity, while a model is referenced, which brings relationships and delete rules, covered in the lesson on SwiftData relationships.
- Make the struct conform to PersistentModel, as the compiler notes suggest.
- SwiftData can persist any struct without an extra conformance.
- The error means SwiftData cannot store custom types at all.
- Marking the model class final resolves the getValue error.
- A Codable struct property is stored as a separate related model.