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

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

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

enum Type: Int, Codable {
    case foo = 0
    case bar = 1
}

@Model
final public class Item {
    var type: Type
}

UI Code

struct DetailView: View {
    @Bindable var item: Item
    
    // MARK: -
    var body: some View {
        Picker("Type", selection: $item.type) {
            Text("Foo").tag(Type.foo)
            Text("Bar").tag(Type.bar)
        }
        .pickerStyle(.segmented)
    }
}
英文:

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

enum Type: Int, Codable {
    case foo = 0
    case bar = 1
}

@Model
final public class Item {
    var type: Type
}

UI Code

struct DetailView: View {
    @Bindable var item: Item
    
    // MARK: -
    var body: some View {
        Picker("Type", selection: $item.type) {
            Text("Foo").tag(Type.foo)
            Text("Bar").tag(Type.bar)
        }
        .pickerStyle(.segmented)
    }
}

答案1

得分: 7

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

enum SomeType: Int, Codable {
    case foo = 0
    case bar = 1
}

@Model
final public class Item {
    
    @Transient var type: SomeType {
        get { SomeType(rawValue: _type)! }
        set { _type = newValue.rawValue }
    }
    
    @Attribute var _type: SomeType.RawValue
    
}
英文:

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

enum SomeType: Int, Codable {
    case foo = 0
    case bar = 1
}

@Model
final public class Item {
    
    @Transient var type: SomeType {
        get { SomeType(rawValue: _type)! }
        set { _type = newValue.rawValue }
    }
    
    @Attribute var _type: SomeType.RawValue
    
} 

答案2

得分: 4

以下是翻译好的内容:

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

enum ItemType: Int, Codable {
    case foo = 0
    case bar = 1
}

@Model
final public class Item {
    
    var type: ItemType
    
    init(type: ItemType) {
        self.type = type
    }
    
}

struct DetailView: View {
    
    @Bindable var item: Item
    
    var body: some View {
        Picker("类型", selection: $item.type) {
            Text("Foo").tag(ItemType.foo)
            Text("Bar").tag(ItemType.bar)
        }
        .pickerStyle(.segmented)
    }
    
}
英文:

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

enum ItemType: Int, Codable {
    case foo = 0
    case bar = 1
}

@Model
final public class Item {
    
    var type: ItemType
    
    init(type: ItemType) {
        self.type = type
    }
    
}

struct DetailView: View {
    
    @Bindable var item: Item
    
    var body: some View {
        Picker("Type", selection: $item.type) {
            Text("Foo").tag(ItemType.foo)
            Text("Bar").tag(ItemType.bar)
        }
        .pickerStyle(.segmented)
    }
    
}

答案3

得分: 0

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

@Model
final public class Item {
    var type: ItemType

    enum ItemType: Int, Codable {
        case foo, bar
    }

    init(type: ItemType) {
        self.type = type
    }
}
英文:

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

@Model
final public class Item {
    var type: ItemType

    enum ItemType: Int, Codable {
        case foo, bar
    }

    init(type: ItemType) {
        self.type = type
    }
}

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:

确定