Problem compiling @Model

I tried coding along with the WWDC23 'Dive Deeper into SwiftData' video but can't get the sample code to compile.

For example the Card class fails with lots of errors associated with @Model. The first of these is: Type 'Card' does not conform to protocol 'PersistentModel'

import SwiftUI
import SwiftData

@Model
final class Card {
    var front: String
    var back: String
    var creationDate: Date

    init(front: String, back: String, creationDate: Date = .now) {
        self.front = front
        self.back = back
        self.creationDate = creationDate
    }
}

On the other hand the following stand alone code (in it's own project) compiles without error. So I am confused and a little fed up with Apple publishing sample code that doesn't compile.

import SwiftData

@Model
final class Card {
    var front: String
    var back: String
    var creationDate: Date

    init(front: String, back: String, creationDate: Date = .now) {
        self.front = front
        self.back = back
        self.creationDate = creationDate
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
    }
}

#Preview {
    ContentView()
}
Answered by Easiwriter in 761676022

The problem is that the downloaded Xcode project is corrupt. I rebuilt the project from scratch using the sources from the download and it compiled OK.

So, Apple, please check the download file.

Accepted Answer

The problem is that the downloaded Xcode project is corrupt. I rebuilt the project from scratch using the sources from the download and it compiled OK.

So, Apple, please check the download file.

Problem compiling @Model
 
 
Q