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

Day 19 Challenge Completed with a Question

Forums > 100 Days of SwiftUI

I just completed coding the Day 19 challenge and I got it to work it appears. I chose to code for the unit of measure length.

When reading the Picker selections for starting and ending UofM I tried to use a switch statements with cases for each picker selection but couldn't get it to build; so eventually abandoned the effort and used If statements instead which works.

Is it possible to use a switch statement instead of If statements? If so how would it be coded?

Much appreciated in advance.

My code follows...

import SwiftUI

struct ContentView: View {
    @State private var inputValue = 0.0
    @State private var inputUnit = ""
    @State private var outputUnit = ""
    var unitsOfMeasure = ["meter","kilomtr", "feet", "yard", "mile"]
    @FocusState private var inputFieldIsFocused: Bool

    var baseUnitValue: Double {
        var valueOfBaseUnit = 0.0
        if inputUnit == "kilomtr" {
            valueOfBaseUnit = inputValue * 1000
        } else if inputUnit == "feet" {
            valueOfBaseUnit = inputValue / 3.2808
        } else if inputUnit == "yard" {
            valueOfBaseUnit = inputValue / 1.0936
        } else if inputUnit == "mile" {
            valueOfBaseUnit = inputValue / 0.00062137
        } else {
            valueOfBaseUnit = inputValue
        }
        return valueOfBaseUnit
    }

    var outputValue: Double {
        var valueOfOutput = 0.0
        if outputUnit == "meter" {
            valueOfOutput = baseUnitValue
        } else if outputUnit == "kilomtr" {
            valueOfOutput = baseUnitValue / 1000
        } else if outputUnit == "feet" {
            valueOfOutput = baseUnitValue * 3.2808
        } else if outputUnit == "yard" {
            valueOfOutput = baseUnitValue * 1.0936
        } else {
            valueOfOutput = baseUnitValue * 0.00062137
        }
        return valueOfOutput
    }

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("Starting Unit", selection: $inputUnit) {
                        ForEach(unitsOfMeasure, id: \.self) {
                            Text($0)
                        }
                    } .pickerStyle(.segmented)
                }
                header: {
                    Text("Select the starting unit of measurement")
                }

                Section {
                    Picker("Ending Unit", selection: $outputUnit) {
                        ForEach(unitsOfMeasure, id: \.self) {
                            Text($0)
                        }
                    }.pickerStyle(.segmented)
                }
                header: {
                    Text("Select the ending unit of measurement")
                }

                Section {
                    TextField("Enter a value to convert", value: $inputValue, format: .number)
                        .keyboardType(.decimalPad)
                        .focused($inputFieldIsFocused)
                }
                header: {
                    Text("Enter a value to convert")
                }
                Section {
                    Text(outputValue, format: .number)
                }
                header: {
                    Text("The converted value")
                }
            }
            .navigationTitle("UnitConverter")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()

                    Button("Done") {
                        inputFieldIsFocused = false
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1      

Good job on getting it working.

Is this what you're looking for?

var baseUnitValue: Double {
        var valueOfBaseUnit = 0.0
        switch inputUnit {
        case "kilomtr":
            valueOfBaseUnit = inputValue * 1000
        case "feet":
            valueOfBaseUnit = inputValue / 3.2808
        case "yard":
            valueOfBaseUnit = inputValue / 1.0936
        case "mile":
            valueOfBaseUnit = inputValue / 0.00062137
        default:
            valueOfBaseUnit = inputValue
        }
        return valueOfBaseUnit
  }

1      

Hey thanks.

Yes I tried that but kept getting an error that there was no return value so punted and went to If statements.

I'll try it again and see what happens.

1      

Try this.

var baseUnitValue: Double {
    switch inputUnit {
    case "kilomtr": return inputValue * 1000
    case "feet": return inputValue / 3.2808
    case "yard": return inputValue / 1.0936
    case "mile": return inputValue / 0.00062137
    default: return inputValue
    }
}

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

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.