英文:
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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论