如何使用SwiftData持久化自定义枚举?

huangapple go评论101阅读模式
英文:

How to persist custom enum with SwiftData?

问题

I want to save a custom enum to my SwiftData model. I can get it to work that it shows and even updates the value in the UI. Though, the change of the enum var/entity isn’t persisted when relaunching the app!

Any advice what to watch out for when using custom enums?

My Model

  1. enum Type: Int, Codable {
  2. case foo = 0
  3. case bar = 1
  4. }
  5. @Model
  6. final public class Item {
  7. var type: Type
  8. }

UI Code

  1. struct DetailView: View {
  2. @Bindable var item: Item
  3. // MARK: -
  4. var body: some View {
  5. Picker("Type", selection: $item.type) {
  6. Text("Foo").tag(Type.foo)
  7. Text("Bar").tag(Type.bar)
  8. }
  9. .pickerStyle(.segmented)
  10. }
  11. }
英文:

I want to save a custom enum to my SwiftData model. I can get it to work that it shows and even updates the value in the UI. Though, the change of the enum var/entity isn’t persisted when relaunching the app!

Any advice what to watch out for when using custom enums?

My Model

  1. enum Type: Int, Codable {
  2. case foo = 0
  3. case bar = 1
  4. }
  5. @Model
  6. final public class Item {
  7. var type: Type
  8. }

UI Code

  1. struct DetailView: View {
  2. @Bindable var item: Item
  3. // MARK: -
  4. var body: some View {
  5. Picker("Type", selection: $item.type) {
  6. Text("Foo").tag(Type.foo)
  7. Text("Bar").tag(Type.bar)
  8. }
  9. .pickerStyle(.segmented)
  10. }
  11. }

答案1

得分: 7

这个解决方案对我有效。希望在未来的SwiftData版本中会修复枚举问题。

  1. enum SomeType: Int, Codable {
  2. case foo = 0
  3. case bar = 1
  4. }
  5. @Model
  6. final public class Item {
  7. @Transient var type: SomeType {
  8. get { SomeType(rawValue: _type)! }
  9. set { _type = newValue.rawValue }
  10. }
  11. @Attribute var _type: SomeType.RawValue
  12. }
英文:

This solution work for me. Hopefully enums will be fixed in future versions of SwiftData.

  1. enum SomeType: Int, Codable {
  2. case foo = 0
  3. case bar = 1
  4. }
  5. @Model
  6. final public class Item {
  7. @Transient var type: SomeType {
  8. get { SomeType(rawValue: _type)! }
  9. set { _type = newValue.rawValue }
  10. }
  11. @Attribute var _type: SomeType.RawValue
  12. }

答案2

得分: 4

以下是翻译好的内容:

问题已在 iOS 版本 17 beta 7 中修复。现在下面的代码可以正常工作。发布说明

  1. enum ItemType: Int, Codable {
  2. case foo = 0
  3. case bar = 1
  4. }
  5. @Model
  6. final public class Item {
  7. var type: ItemType
  8. init(type: ItemType) {
  9. self.type = type
  10. }
  11. }
  12. struct DetailView: View {
  13. @Bindable var item: Item
  14. var body: some View {
  15. Picker("类型", selection: $item.type) {
  16. Text("Foo").tag(ItemType.foo)
  17. Text("Bar").tag(ItemType.bar)
  18. }
  19. .pickerStyle(.segmented)
  20. }
  21. }
英文:

The issue has been fixed since iOS version 17 beta 7. Now the code below works correctly. Release notes

  1. enum ItemType: Int, Codable {
  2. case foo = 0
  3. case bar = 1
  4. }
  5. @Model
  6. final public class Item {
  7. var type: ItemType
  8. init(type: ItemType) {
  9. self.type = type
  10. }
  11. }
  12. struct DetailView: View {
  13. @Bindable var item: Item
  14. var body: some View {
  15. Picker("Type", selection: $item.type) {
  16. Text("Foo").tag(ItemType.foo)
  17. Text("Bar").tag(ItemType.bar)
  18. }
  19. .pickerStyle(.segmented)
  20. }
  21. }

答案3

得分: 0

你是否在类内部放置枚举时遇到相同的问题?

  1. @Model
  2. final public class Item {
  3. var type: ItemType
  4. enum ItemType: Int, Codable {
  5. case foo, bar
  6. }
  7. init(type: ItemType) {
  8. self.type = type
  9. }
  10. }
英文:

Are you witnessing the same issue with the enum placed inside of the class?

  1. @Model
  2. final public class Item {
  3. var type: ItemType
  4. enum ItemType: Int, Codable {
  5. case foo, bar
  6. }
  7. init(type: ItemType) {
  8. self.type = type
  9. }
  10. }

huangapple
  • 本文由 发表于 2023年6月15日 03:03:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定