SwiftData crashes when fetching certain types of enum cases

Number:rdar://FB13408892 Date Originated:2023-11-24
Status:Closed Resolved:iOS 18
Product:SwiftData Product Version:iOS 17.2 Seed 3 (21C5046c)
Classification:Incorrect/Unexpected Behavior Reproducible:Always
I have a model class that has a persistent property for a `Codable` enum. If the enum type is written a certain way, the app will crash because SwiftData encounters the following fatal error: `Could not cast value of type 'Swift.Optional<Any>' (0x1f5b7ff50) to 'SwiftDataCodableEnumCrash.Item.NestedEnum' (0x102a14cd0)`. The full stack trace is attached.

I have attached a minimal sample project reproducing the issue. `Item.swift` is annotated with comments explaining the significance of certain pieces of code. Below is a summary of the conditions that are needed to trigger the SwiftData bug:
- There needs to be a nested enum.
- Within the nested enum, there needs to be a case without an associated value.



Please verify this issue with iOS 18 beta and update your bug report with your results by logging into https://feedbackassistant.apple.com/ or by using the Feedback Assistant app.

iOS 18 beta (Build: 22A5282m) https://developer.apple.com/download/

If the issue persists, please attach a new sysdiagnose captured in the latest build and attach it to the bug report.

iOS sysdiagnose Instructions: https://developer.apple.com/services-account/download?path=/iOS/iOS_Logs/sysdiagnose_Logging_Instructions.pdf

If this is no longer an issue you can close this feedback by selecting “Close Feedback” via the Actions button in Feedback Assistant.

For a complete list of logging instructions visit: https://developer.apple.com/bug-reporting/profiles-and-logs/

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!