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:
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?
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 #Preview
s 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.