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

Variable not getting updated

Forums > SwiftUI

Hi there -

Newbie to swift and computer languages with a question about why a particular variable is not updating.

So I'm creating a unit converter app from Challenge Day 19.

I'm using Measurement() to do it. So the basics I have are:

my variables:

    @State var fromAmount = 0.0
    @State var adjustedFromAmount = ""
    @State var toAmount = 0.0
    @State var adjustedToAmount = Measurement(value: 0.0, unit: UnitLength .meters)
    @State var selectedFromUnit = ""
    @State var selectedToUnit = ""
    @State var figuredToUnit: String = ""

my function:

func convertTo(selectedFrom: String, selectedTo: String) {
        if selectedFrom == "meters" {
            switch selectedTo {
            case "feet":
               adjustedToAmount = Measurement(value: fromAmount, unit: .meters).converted(to: UnitLength.feet)
                figuredToUnit = "1"
                toAmount = adjustedToAmount.value
            case "inches":
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.meters).converted(to: UnitLength.inches)
                figuredToUnit = "2"
                toAmount = adjustedToAmount.value
            default:
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.meters)
                figuredToUnit = "3"
                toAmount = adjustedToAmount.value
            }
        } else if selectedFrom == "feet" {
            switch selectedTo {
            case "meters":
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.feet).converted(to: UnitLength.meters)
                figuredToUnit = "4"
                toAmount = adjustedToAmount.value
            case "inches":
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.feet).converted(to: UnitLength.inches)
                figuredToUnit = "5"
                toAmount = adjustedToAmount.value
            default:
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.feet)
                figuredToUnit = "6"
                toAmount = adjustedToAmount.value
            }
        } else if selectedFrom == "inches" {
            switch selectedTo {
            case "feet":
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.inches).converted(to: UnitLength.feet)
                figuredToUnit = "7"
                toAmount = adjustedToAmount.value
            case "meters":
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.inches).converted(to: UnitLength.meters)
                figuredToUnit = "8"
                toAmount = adjustedToAmount.value
            default:
                adjustedToAmount = Measurement(value: fromAmount, unit: UnitLength.inches)
                figuredToUnit = "9"
                toAmount = adjustedToAmount.value
            }
        }

    }

My view looks like:

                    Text("Enter Amount to Convert")
                    TextField("Enter Amount to:", value: $fromAmount, format: .number)
                        .frame(width: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/)
                        .multilineTextAlignment(.trailing)
                        .border(Color.gray)
                        .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
                    Text("Select a From Unit")
                    Picker("Select Unit to Convert From", selection: $selectedFromUnit) {
                        ForEach(listOfUnits, id: \.self) {
                            Text($0)
                        }
                    }
                  /* .onChange(of: selectedFromUnit){
                     convertTo(selectedFrom: selectedFromUnit, selectedTo: selectedToUnit)
                    }*/
                    .pickerStyle(.segmented)
                    Text("Select a To Unit")
                    Picker("Select Unit to Convert To", selection: $selectedToUnit) {
                            ForEach(listOfUnits, id: \.self) {
                                Text($0)
                            }
                        }
                     /*   .onChange(of: selectedToUnit){
                            convertTo(selectedFrom: selectedFromUnit, selectedTo: selectedToUnit)
                        }*/
                        .pickerStyle(.segmented)
                    Button("Convert"){
                        convertTo(selectedFrom: selectedFromUnit, selectedTo: selectedToUnit)
                    }

I call the function in a button:

Button("Convert"){
                        convertTo(selectedFrom: selectedFromUnit, selectedTo: selectedToUnit)
                    }

Then I uses Text() to show my results:

Text("\(adjustedToAmount.formatted(unitStyle))")
                    .font(.largeTitle)
                Text("\(figuredToUnit)")
                Text("\(toAmount)")

The funny thing that happens is as I start changing my units it stops converting. So the variable adjustedToAmount stops updating, but the variable figuredToUnit continues to show the correct evaulation as I change my From/To units.

As you can I tried the onChange() but that is when I first started noticing the issue. I thought maybe a button would fix it, but it hasn't.

Look forward to learning what is the cause of this and more about swift.

Thanks,

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

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.