TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

DAY 19. Unit conversion.

Forums > 100 Days of SwiftUI

I thought to make the conversion shorter this way. It didn't work. The outputNumber doesn't update and doesn't make coversion when I prompt number into TextField. Why it doesn't work?

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

    let unitsList = ["meters", "kilometers", "feet", "yards", "miles"]

    var outputNumber: Measurement<UnitLength> {
        let inputValue = Double(inputNumber) ?? 0

        return Measurement(value: inputValue, unit: UnitLength.init(symbol: unitsList[inputUnit])).converted(to: UnitLength.init(symbol: unitsList[outputUnit]))

    }

Switch implemenatation that works

    var imperialUnitsInMeters: Measurement<UnitLength> {
        let inputValue = Double(inputNumber) ?? 0

        switch unitsList[inputUnit] {
        case "meters":
            return Measurement(value: inputValue, unit: UnitLength.meters)
        case "kilometers":
            return Measurement(value: inputValue, unit: UnitLength.kilometers).converted(to: UnitLength.meters)
        .........................................
        .........................................
        default:
            return Measurement(value: inputValue, unit: UnitLength.meters)
        }
    }

    var outputNumber: Measurement<UnitLength> {
        let inputValue = Double(imperialUnitsInMeters.value)

        switch unitsList[outputUnit] {
        case "meters":
            return Measurement(value: inputValue, unit: UnitLength.meters)
        case "kilometers":
            return Measurement(value: inputValue, unit: UnitLength.meters).converted(to: UnitLength.kilometers)
        .........................................
        .........................................
        default:
            return Measurement(value: inputValue, unit: UnitLength.meters)
        }
    }

3      

I haven't used the init(symbol:) before, but if it works, then you should use the correct symbols in the array, so ["m", "km"] and so on...

But the recommendation is to use an array of UnitLength instead of String for your units.

4      

@MarcusKay , I checked this out. Didn't work.

      let unitsList = ["m", "km", "ft", ...]

This shows the correct units; also, units change when switching the picker choice, but the value reamins 0


      Text("\(outputNumber.description)")

3      

Well then your solution is to use an array of UnitLength:

let unitsList: [UnitLength] = [.meters, .kilometers, ...]

For your picker you can then use the symbol to display.

We discussed this in depth over on this thread if you want more information: https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-19-challenge-is-there-a-better-way-for-this-code/2682

4      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.