Post

Replies

Boosts

Views

Activity

SwiftUI Picker with option for no selection?
Is it possible to create a Picker that gives the user to not make a choice? Here is my existing code:  @FetchRequest(entity: ItemCategory.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \ItemCategory.name, ascending: true)]) var categories: FetchedResults&lt;ItemCategory&gt; @State private var selectedCategoryIndex = 0 /* omitted */ Section(header: Text("Category")) { Picker(selection: $selectedCategoryIndex, label: Text("Category")) { ForEach(0 ..< categories.count) { if (categories[$0].name != nil) { Text(categories[$0].name!) } } } } This code allows the user to pick an option, but because I set the selection index to 0 it forces that choice without further interaction. When I use an optional for the index, it breaks selection completely. No item can be selected in that case. Additionally, 'categories' is a FetchedResults collection.
2
1
6.7k
Jun ’20