表单中的SwiftUI Picker不显示CoreData的选中标记

时间:2019-12-30 15:29:49

标签: core-data swiftui picker

我有一个问题,在选择器中选择一个值后,我没有看到它被选中:

> dt %>% pivot_wider(-c(transcript_id, variant_id), names_from = "is_NL", values_from = paste0(dt$counts, ";", dt$nrows), names_prefix = "NL_") %>% as.data.table
Error: Unknown columns `32968;685`, `1440;20`, `337;1`, `33157;690`, `1251;15` and ... 
Run `rlang::last_error()` to see where the error occurred.

“我的核心数据”具有以下两个属性:

struct SampleView: View {
    @FetchRequest(
        entity: Category.entity(),
        sortDescriptors: [
            NSSortDescriptor(keyPath: \Category.title, ascending: true)
        ]

    ) var categories: FetchedResults<Category>
    @State private var category = UUID()

    var body: some View {
        NavigationView {
        VStack {
            Form {
                Picker(selection: $category, label: Text("Picker")) {
                    ForEach(categories) { cat in
                        Text(cat.title!)
                    }
                }
            }
        }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

Picker中的选择应与显示的值相同,因此您需要类似

@State private var category: UUID? = UUID()

注意:可能是由于并非我示例中提供的所有代码,我需要按如下所示更改代码以使其可编译(已通过Xcode 11.2测试)

ForEach(categories, id: \.id) { cat in