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

SOLVED: Day 19: Challenge Day

Forums > 100 Days of SwiftUI

I am having hard time to understand and excute the messuermesnt coding part, can someone explain it to me please

I have looked into to How to convert units using Unit and Measurement article but i am still stuck and tried several times and still not working.

struct ContentView: View {

    @State private var inputNumber = ""
    @State private var inputUnit   = 0
    @State private var outputUnit  = 0

    let conversionUnit  = ["Kilometers", "Miles", "Meters"]

    var totalconversion: Double {
        let total = Double(inputNumber) ?? 0

        return 0

    }

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Please enter the amount")) {

                    TextField("Enter here", text: $inputNumber)
                        .keyboardType(.decimalPad)
                    Text("Choose Your Input Unit")
                    Picker("From Unit", selection: $inputUnit) {
                        ForEach(0 ..< conversionUnit.count) {
                            Text("\(self.conversionUnit[$0])")
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }       .textCase(nil)

                Section {
                    Text("Choose Your Output Unit")
                    Picker("To Unit", selection: $outputUnit) {
                        ForEach(0 ..< conversionUnit.count) {
                            Text("\(self.conversionUnit[$0])")
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())

                    Text("\(outputUnit, specifier: "%.2f")")

                }
            }
            .navigationBarTitle("Unit Converter")
        }

    }
}

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

3      

You can convert from one Measurement to another. Which means you first must take the input and turn it into a specific unit of measurement.

So if the user enters 10, you need to be able to convert it to an actual unit. Something like this:

let baseUnit = Measurement(value: Double(inputUnit) ?? 0, unit: UnitLength.kilometers)

//This allows you to then convert like so

let finalUnit = baseUnit.converted(to: UnitLength.miles)

The user inputs a number which we get as a String, they then set the initial measurement unit and final one. So we need to turn the String into a Double so we can apply math or use it like above, then based on their initial and final units, choose the right conversion.

That should get you going I hope.

edit: One more thing to note here is that if you want to be able to format the result of the conversion you should use:

let finalUnit = baseUnit.converted(to: UnitLength.miles).value

This will be a Double.

4      

@MarcusKay thank you i finally figured it out

4      

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.