TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.