#Predicate doesn't work with enum

Problem

The following code doesn't work:

let predicate = #Predicate<Car> { car in
   car.size == size //This doesn't work
}

Console Error

Query encountered an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.unsupportedPredicate)

Root cause

Size is an enum, #Predicate works with other type such as String however doesn't work with enum Enum value is saved however is not filtered by #Predicate

Environment

  • Xcode: 15.0 (15A240d) - App Store
  • macOS: 14.0 (23A339) - Release Candidate

Steps to reproduce

  1. Run the app on iOS 17 or macOS Sonoma
  2. Press the Add button
  3. Notice that the list remains empty

Expected behaviour

List should show the newly created small car

Actual behaviour

List remains empty inspite of successfully creating the small car.

Feedback

FB13194334

Code

Size

enum Size: String, Codable {
    case small
    case medium
    case large
}

Car

import SwiftData

@Model
class Car {
    let id: UUID
    let name: String
    let size: Size
    
    init(
        id: UUID,
        name: String,
        size: Size
    ) {
        self.id = id
        self.name = name
        self.size = size
    }
}

ContentView

struct ContentView: View {

    var body: some View {
        NavigationStack {
            CarList(size: .small)
        }
    }

CarList

import SwiftUI
import SwiftData

struct CarList: View {
    let size: Size
    
    @Environment(\.modelContext)
    private var modelContext

    @Query
    private var cars: [Car]
    
    init(size: Size) {
        self.size = size
        let predicate = #Predicate<Car> { car in
            car.size == size //This doesn't work
        }
        _cars = Query(filter: predicate, sort: \.name)
    }
    
    var body: some View {
        List(cars) { car in
            VStack(alignment: .leading) {
                Text(car.name)
                Text("\(car.size.rawValue)")
                Text(car.id.uuidString)
                    .font(.footnote)
            }
        }
        .toolbar {
            Button("Add") {
                createCar()
            }
        }
    }
    
    private func createCar() {
        let name = "aaa"
        let car = Car(
            id: UUID(),
            name: name,
            size: size
        )
        modelContext.insert(car)
    }
}
Post not yet marked as solved Up vote post of newwbee Down vote post of newwbee
1k views

Replies

I forgot to add the code for App, any help on this would be much appreciated.

SwiftDataEnumDemoApp

import SwiftUI
import SwiftData

@main
struct SwiftDataEnumDemoApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .modelContainer(for: Car.self)
        }
    }
}

I am seeing the same behavior too...It doesn't seem to work with enums at all When I compare the enums I get the following in the console

Query encountered an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.unsupportedPredicate)

When i try to compare the enums using their raw value i get the following crash reffering to a file within the framework


SwiftData/DataUtilities.swift:1140: Fatal error: Unexpected type for Expansion: Optional<UIColor>

The UIColor is reffering to a color that i have as a transformable property

It would be great if this was resolved before the GM release

Im having the same issue, haven't found a fix anywhere yet though.