I'm trying #swift and #swiftdata for the first time, and need help with an error.

I get: SwiftData/BackingData.swift:201: Fatal error: expected attribute to be Codable

while inserting this model into a context

@Model
final class Item {
var name: String
var kind: Kind

enum Kind: String, Codable {
case function
case module
}

init(name: String, kind: Kind) { ... }
}

Removing kind from the model fixes it.

Is this even my bug? Or a SwiftData issue?

@GregMorenz

Where in your files does the error appear? I’m not able to reproduce.

Are you using XCode beta 2?

@kivo

Yes, I'm using XCode beta 2 (Version 15.0 beta 2 (15A5161b))

Note it's a runtime error not a compile time one.

The actual error prints in x-code after this line of code, when you click the button:

```
Button("+", action: {modelContext.insert(Item(name: "test", kind: Item.Kind.function))})
```

In the meantime I've learned two new things:

1/5

1) The contents of the init function matters, it's

```
init(name: String, kind: Kind) {
self.name = name
self.kind = kind
}
```

Commenting out `self.kind = kind` avoids the crash.

2) I'm starting this in a DocumentGroup, using a `WindowGroup{ ... }.modelContainer(for: Item.self)` avoids the crash

```
DocumentGroup(editing: Item.self, contentType: .rot) {
ContentView()
}
```

Source follows

2/5

```
import SwiftUI
import SwiftData

@main
struct MainApp: App {
var body: some Scene {
DocumentGroup(editing: Item.self, contentType: .rot) {
ContentView()
}
}
}

struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var items: [Item]

var body: some View {
Button("+", action: {modelContext.insert(Item(name: "test", kind: Item.Kind.function))})
}
}
```

3/5

```
@Model
final class Item {
var name: String
var kind: Kind

enum Kind: Int, Codable {
case function
case module
}

init(name: String, kind: Kind) {
self.name = name
self.kind = kind
}
}

import UniformTypeIdentifiers

extension UTType {
static var rot = UTType(exportedAs: "cafe.droid.rot")
}
```

Filetype set up in xcode as described here: https://developer.apple.com/videos/play/wwdc2023/10154?time=939

4/5

Build an app with SwiftData - WWDC23 - Videos - Apple Developer

Discover how SwiftData can help you persist data in your app. Code along with us as we bring SwiftData to a multi-platform SwiftUI app...

Apple Developer

Totally understand I'm probably throwing more info at you here than you probably really want. I'm pretty much convinced that this is just an apple bug, and not a case of me holding it wrong.

I appreciate you looking at it, but don't feel obliged to.

5/5

@kivo Hmm, not sure i needed to tag you on replies 2 through 5, they should be there if you click on the first reply I think.

@GregMorenz It’s all good, thanks for sharing. I do know that there are some unreleased fixes to the compiler that have yet to be incorporated into the Beta.

I’ve been playing with it myself for about a month and faced a number of issues with #swiftdata. But, in general, I appreciate the simplification of the syntax and the ease of declaring model classes and fetching from them.