Skip to content

SwiftData + PassColor causes __NSCFNumber to NSString casting crash #22

Description

@mohalibou

When using SwiftData + PassColor, I encounter a consistent crash:

Could not cast value of type '__NSCFNumber' to 'NSString'

This happens when saving a model with an optional PassColor? and then fetching it back.

The crash seems to occur here.

Below is a code sample you can copy/paste that will allow you to easily reproduce the same issue:

import PassCore
import SwiftUI
import SwiftData

@Model
final class WalletPass {
    var name: String
    var labelColor: PassColor?

    init(name: String, labelColor: PassColor?) {
        self.name = name
        self.labelColor = labelColor
    }
}

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

    var body: some View {
        List {
            Button("Insert Pass") {
                let pass = WalletPass(
                    name: "Some Pass",
                    labelColor: .init(r: 255, g: 0, b: 0)
                )
                modelContext.insert(pass)
                try? modelContext.save()
            }
            ForEach(passes) { pass in
                Text(pass.name)
            }
        }
    }
}

@main
struct TestingPassColorCrashApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: [WalletPass.self])
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions