WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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()
    }
}

   

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
  }

   

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.

   

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
    }
}

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, 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!

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.