I am trying to create a menu picker for two or three text items. Small miracles, but I have it basically working. Problem is it uses a set, and I want to pass arrays. I need to modify PickerView so the Bound Parameter is an [String] instead of Set<String>. Have been fighting this for a while now... Hoping for insights.
struct PickerView: View {
@Binding var colorChoices: Set<String>
let defaults = UserDefaults.standard
var body: some View {
let possibleColors = defaults.object(forKey: "ColorChoices") as? [String] ?? [String]()
Menu {
ForEach(possibleColors, id: \.self) { item in
Button(action: {
if colorChoices.contains(item) {
colorChoices.remove(item)
} else {
colorChoices.insert(item)
}
}) {
HStack {
Text(item)
Spacer()
if colorChoices.contains(item) {
Image(systemName: "checkmark")
}
}
}
}
} label: {
Label("Select Items", systemImage: "ellipsis.circle")
}
Text("Selected Colors: \(colorChoices, format: .list(type: .and))")
}
}
#Preview("empty") {
@Previewable @State var colorChoices: Set<String> = []
PickerView(colorChoices: $colorChoices)
}
#Preview("Prefilled") {
@Previewable @State var colorChoices: Set<String> = ["Red","Blue"]
PickerView(colorChoices: $colorChoices)
}
My Content View is suppose to set default values the first time it runs, if no values already exist...
import SwiftUI
struct ContentView: View {
@State private var viewDidLoad: Bool = false
var body: some View {
HomeView()
.onAppear { // The following code should execute once the first time contentview loads. If a user navigates back to it, it should not execute a second time.
if viewDidLoad == false {
viewDidLoad = true
// load user defaults
let defaults = UserDefaults.standard
// set the default list of school colors, unless the user has already updated it prior
let defaultColorChoices: [String] = ["Black","Gold","Blue","Red","Green","White"]
let colorChoices = defaults.object(forKey: "ColorChoices") as? [String] ?? defaultColorChoices
defaults.set(colorChoices, forKey: "ColorChoices")
}
}
}
}
#Preview {
ContentView()
}
PickLoader allows you to dynamically add or delete choices from the list...
import SwiftUI
struct PickLoader: View {
@State private var newColor: String = ""
var body: some View {
Form {
Section("Active Color Choices") {
// we should have set a default color list in contentview, so empty string should not be possible.
let defaults = UserDefaults.standard
let colorChoices = defaults.object(forKey: "ColorChoices") as? [String] ?? [String]()
List {
ForEach(colorChoices, id: \.self) { color in
Text(color)
}
.onDelete(perform: delete)
HStack {
TextField("Add a color", text: $newColor)
Button("Add"){
defaults.set(colorChoices + [newColor], forKey: "ColorChoices")
newColor = ""
}
}
}
}
}
.navigationTitle("Load Picker")
Button("Reset Default Choices") {
let defaults = UserDefaults.standard
//UserDefaults.standard.removeObject(forKey: "ColorChoices")
let colorChoices: [String] = ["Black","Gold","Blue","Red","Green","White"]
defaults.set(colorChoices, forKey: "ColorChoices")
}
Button("Clear all choices") {
let defaults = UserDefaults.standard
defaults.removeObject(forKey: "ColorChoices")
}
}
}
func delete(at offsets: IndexSet) {
let defaults = UserDefaults.standard
var colorChoices = defaults.object(forKey: "ColorChoices") as? [String] ?? [String]()
colorChoices.remove(atOffsets: offsets)
defaults.set(colorChoices, forKey: "ColorChoices")
}
#Preview {
PickLoader()
}
And finally HomeView is where I am testing from - to see if binding works properly...
import SwiftUI
struct HomeView: View {
//@State private var selection: Set<String> = []
//@State private var selection: Set<String> = ["Blue"]
@State private var selection: Set<String> = ["Blue", "Red"]
var body: some View {
NavigationStack {
List {
Section("Edit Picker") {
NavigationLink("Load Picker") {
PickLoader()
}
}
Section("Test Picker") {
PickerView(colorChoices: $selection)
}
Section("Current Results") {
Text("Current Selection: \(selection, format: .list(type: .and))")
}
}
.navigationBarTitle("Hello, World!")
}
}
}
#Preview {
HomeView()
}
If anyone uses this code, there are still issues - buttons on Loader don't update the list on the screen for one, and also dealing with deleting choices that are in use - how does picker deal with them? Probably simply add to the list automatically and move on. If anyone has insights on any of this also, great! but first I just need to understand how to accept an array instead of a set in pickerView.
I have tried using a computed value with a get and set, but I can't seem to get it right.
Thanks for any assistance! Cheers!