BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SOLVED: Initializing a Dictionary to use in a Picker

Forums > SwiftUI

The "newLetterScoreRules" in this View does not get initialized with the current values. I suspect when it does I will also have an issue with the Picker as it does not behave as I would expect showing new values in the UI

import SwiftUI

struct PenSetTest: View {
    @Environment(\.dismiss) var dismiss

    let regatta: Regatta
    let priorLetterScoreRules: [RaceLetterScore : Double]
    @State private var newLetterScoreRules: [RaceLetterScore : Double]

    init(regatta : Regatta) {
        self.regatta = regatta
        priorLetterScoreRules = regatta.letterScoreRules
        _newLetterScoreRules = State(initialValue: regatta.letterScoreRules)
    }

    var body: some View {
        Form {
            ForEach(RaceLetterScore.allCases, id: \.id ) { letterScore in
                Picker("\(letterScore.rawValue)", selection: $newLetterScoreRules[letterScore]) {
                    ForEach(0..<10) {
//                        Text($0, format: .number)
                        Text("\($0)").tag($0)
                    }
                }
            }
        }
    }
}

2      

It turns out it was being initialized but the Picker was failing at run time because the tag was not an optional. This code works:

import SwiftUI

struct PenaltySettingsView: View {
    @EnvironmentObject var regattaList : RegattaList
    @Environment(\.dismiss) var dismiss

    let regatta: Regatta
    let priorLetterScoreRules: [RaceLetterScore : Double]
    let penaltyScores: [Double] = [0,1,2,3,4,5,6,7,8,9]

    @State private var newLetterScoreRules: [RaceLetterScore : Double]

    init(regatta : Regatta) {
        self.regatta = regatta
        priorLetterScoreRules = regatta.letterScoreRules
        _newLetterScoreRules = State(initialValue: regatta.letterScoreRules  )
    }

    var body: some View {
        Form {
            HStack{
                Text("Penalty Settings").font(.title)
                Spacer()
                Button("Save") {
                    regatta.letterScoreRules = newLetterScoreRules
                    regatta.updateAllScores(oldLSR: priorLetterScoreRules)
                    regattaList.save()
                    dismiss()
                }
            }
            ForEach(RaceLetterScore.allCases.filter( { !manualLetterScores.contains($0)}), id: \.id ) { ls in
                Picker("\(ls.rawValue) ", selection: $newLetterScoreRules[ls]) {
                    ForEach(penaltyScores, id: \.self) { ps in
                        Text("\(ps, specifier: "%.0f")").tag(ps as Double?)
                    }
                } 
            }
        }
    }

}

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.