Skip to content

Instantly share code, notes, and snippets.

@kishikawakatsumi
Created July 17, 2023 09:34
Show Gist options
  • Save kishikawakatsumi/a7fe19004afe10069cf6c23215668520 to your computer and use it in GitHub Desktop.
Save kishikawakatsumi/a7fe19004afe10069cf6c23215668520 to your computer and use it in GitHub Desktop.
[SwiftData] Compiler cannot reject models containing types that cannot be saved.
SwiftData raises a runtime error when attempting to save a property of CGFloat type, but the compiler cannot validate it.
Models with properties of type CGFloat, such as the example below, cannot be saved in SwiftData. It will cause a run-time error.
I believe that the compiler should reject such incorrect models. However, such models cannot be made an error at compile-time, so the following documentation statement that SwiftData can safely describe the model layer is highly questionable.
> Using modern language features like macros, SwiftData enables you to write code that is fast, efficient, and safe, enabling you to describe the entire model layer (or object graph) for your app.
```swift
@Model
final class Point {
var x: CGFloat
var y: CGFloat
init(x: CGFloat, y: CGFloat) {
self.x = x
self.y = y
}
}
```
@kishikawakatsumi
Copy link
Author

I have confirmed that this has been fixed in iOS 17.1 beta2. For your information, in 17.1 beta1, when I tried to save CGFloat, it always saved zero value without any error.

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