0

I'm trying to make a mixed picker in swift ui. Means: first part is made of values from a constant, second part is made of core data entity values.

My Code so far

  1. Structs
struct StandardArten: Identifiable, Hashable {
    var id: UUID = UUID()
    var name: String
}

struct Constants {
    static let standardKontakte = [
        StandardArten(name: "Hausarzt")
    ]
}
  1. picker
Picker("Kategorie", selection: $kategorie) {
    Text("Bitte wählen...").tag(Optional<UUID>(nil))
    ForEach(Constants.standardKontakte, id: \.id) { kat in
        Text(kat.name).tag(kat.id)
    }
    Divider()
    ForEach(adressKategorien, id:\.id) { kat in
        Text(kat.name ?? "ohne Titel").tag(kat.id)
    }
}

The picker is displayed properly (see image), but selecting "Hausarzt" the picker "jumps back" to the old value.

picker displayed properly

So what am I doing wrong here?

Additional information: $kategorie is declared as @State var kategorie: UUID?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
mihema
  • 158
  • 1
  • 10
  • Does this answer your question? [Picker for optional data type in SwiftUI?](https://stackoverflow.com/questions/59348093/picker-for-optional-data-type-in-swiftui) – Nirav D Feb 02 '23 at 08:25
  • 1
    Yes, it does. Thank you. Just as solution posted here: ```Text(kat.name).tag(kat.id as UUID?)``` in standardKontakte does the job – mihema Feb 02 '23 at 12:43

0 Answers0