Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve SwiftData error "Type '*' does not conform to protocol 'PersistentModel'"

Working through the betas of SwiftData and trying to stand up a PersistentContainer. I set up a modelContainer View modifier on my ContentView and conformed my class to @Model as described in various WWDC videos (the ones that are out).

Here is the base object I want to store, where all the variables conform to Codable:

Board.swift

import SwiftData

@Model
class Board {
    var size: Int = 3
    var cellSize: CGFloat = 44
    var numberOfTeamMembers: Int = 3
    var numberOfEnemies: Int = 3
    var spaces: [[Space]] = []
    var selectedSpace: Space? = nil
    var characters: [Space: Character] = [:]
    var selectedCharacter: Character? = nil
    var attackableSpaces: [Space] = []
    var movableSpaces: [Space] = []
    var teams: [Team] = []
    var localTeam: Team? = nil
    var currentTeam: Team? = nil
    var gamePhase: GamePhase = GamePhase.pickTeam
    var actions: [Action] = []
    var unoccupiedSpaces: [Space] = []
    var tilt: CGFloat = 1
    var availableMoves: [UUID: [TurnPhase]] = [:]
    var turn: Turn = Turn.mine
}

ContentView.swift

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: [Board.self])
    }
}

I'm seeing the following compilation errors:

SwiftData errors

Type 'Board' does not conform to protocol 'PersistentModel'

No exact matches in call to instance method 'setValue'

Tried implementing as documentation describes, but it doesn't quite work. Where did I go wrong?

like image 680
Brianna Doubt Avatar asked Dec 04 '25 17:12

Brianna Doubt


2 Answers

Your implementation is correct, but there is a bug in the Swift compiler (now fixed but not yet in Xcode 15.0 beta 2) that prevents this from working. The bug is related to the visibility of macro expansions from other scopes so the work around is to remove the #Previews on Views that take macro'd (e.g. @Model) types as arguments.

Note: credit for this discovery and workaround go to Holly Borla.

Update: This appears to be fixed in Xcode 15.0 beta 3.

like image 95
Ben Hale Avatar answered Dec 07 '25 05:12

Ben Hale


I've got the exact same errors:

No exact matches in call to instance method 'setValue'
No exact matches in call to instance method 'getValue'

In my case the problem was the types in the models. I've defined a few enums to help me reason about certain things. But these types weren't marked with the @Model macro. And somehow the compiler couldn't resolve the macros around these and has thrown the errors as above.

For me what worked, was simply changing in the model the type from enum to its rawValue type and save that instead. Possibly one of your types Space, Character, Team... also uses an enum or other unsupported type?

Overall changing the types to a basic ones (like Int, Double, String etc) is less than ideal solution, possibly there's a better workaround, but for a month old technology, I can continue exploring what's there and hopefully we'll get some better examples on how to store things.

like image 40
mikett Avatar answered Dec 07 '25 07:12

mikett