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 See if you can debug it using ChatGPT! It may tell you the issue, it does really well with identification of issues and explaining them!

@AndrewMettier SwiftData and the error message are both new (Google still returns no results found for "Fatal error: expected attribute to be Codable"), ChatGPT is... unlikely... to have the right context to help since it's training data is all from before any of this existed.

On a basic attempt it returns something confident sounding, but that does not fix the issue... and doesn't really make sense if you stop to think about it: https://chat.openai.com/share/a48b19e9-122f-406e-8218-daada705594f

ChatGPT

A conversational AI system that listens, learns, and challenges

@GregMorenz pretty sure the release notes mention that string-backed enums are not currently supported. Have also seen posts on the dev forums about enums not working, period. Haven’t tried it myself, though.

@sfko I admit to not reading the release notes, but I tried Int backed first, so it shouldn't just be the prior. The latter sounds pretty plausible.

The forums you're referring to are these ones? https://forums.swift.org/

Swift Forums

Swift Forums

Swift Forums
Error saving @Model with enum | Apple Developer Forums

@GregMorenz Release notes list issues with enum using String as the RawRepresentable type. If you changed it to Int it will start working. It’s a known issue so should hopefully be fixed soon.

@jonduenas Someone else mentioned this, but the exact same code with `Kind: String` replaced with `Kind: Int` results in the exact same error.

It's actually what I tried first.

@jonduenas Still, there were other people running into similar (though not identical) issues on the dev forums (also kindly linked by the other reply). So I'm just assuming that this will be fixed with that for now.
@GregMorenz Ah dang, definitely file feedback on it!

@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.

@GregMorenz Did you ever find a solution to this problem? I'm running into the same thing, with a model that has a custom structure as an attribute.

@natemartinsf I didn't. I concluded that enum's just weren't properly supported in SwiftData and moved on without them in the model struct.

Which was fine for the app I was prototyping, the enum I wanted was just describing a string in the data model anyways, so I can calculate it at runtime. It certainly seems like a pretty big oversight from apple though.

@GregMorenz Yeah, I hope they fix it. I found a similar workaround for enums (store the raw value and create the enum as needed)

Unfortunately I’m running into the same issue with including a custom struct in my model, which I haven’t found a workaround yet.

Severely limits the ability to do any sort of complex model unfortunately. I guess I’ll wait for beta 6!