UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.