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

Challenge 1 (day 19)

Forums > SwiftUI

@SBvt  

I choose to convert the temperature Units. I tried to convert the initial value to celsius as to be able to convert it to the expected value, but I couldn’t get it :

struct ContentView: View { @State private var checkValue = "" @State private var initialTemperatureUnit = 0 let tipInitialTemperature = ["Celsius", "Fahrenheit", "Kelvin"] @State private var finalTemperatureUnit = 0 let tipFinalTemperature = ["Celsius", "Fahrenheit", "Kelvin"] let valueUnit = Double(checkValue) ?? 0 let celsiusValue = Measurement(value: valueUnit, unit: UnitTemperature.celsius) let farenheintValue = Measurement(value: valueUnit, unit: UnitTemperature.fahrenheit) let kelvinValue = Measurement(value: valueUnit, unit: UnitTemperature.kelvin) let temperatureSelection = Double(tipInitialTemperature[initialTemperatureUnit]) ?? 0 var celsiusInitialValue = Double { if temperatureSelection == 0 { let celsiusInitialValue = celsiusValue.converted(to: UnitTemperature.celsius) } else if initialTemperatureUnit == 0 { let celsiusInitialValue = farenheintValue.converted(to: .celsius) } else if initialTemperatureUnit == 2 { let celsiusInitialValue = kelvinValue.converted(to: .celsius) } return celsiusInitialValue }

Can someone help me ?

4      

I had a little trouble reading through the code in your question but I also did that project in 100DaysOfSwiftUI. I was converting different values but the principle is of course the same.

You can use a switch statement to switch on multiple items. I wrote up what I think is correct logic and math for your conversions but you might want to check my work. I didn't work through the View but you'd likely want to use a TextField and bind the text to your checkValue. Then a couple pickers to state what type of value the user has entered and what type of value they want to convert to. If you have questions on that feel free to ask. Hopefully this helps.

struct ContentView: View {
    @State private var checkValue = ""
    @State private var initialTemperatureUnit = 0
    @State private var finalTemperatureUnit = 0

    let tipInitialTemperature = ["Celsius", "Fahrenheit", "Kelvin"]
    let tipFinalTemperature = ["Celsius", "Fahrenheit", "Kelvin"]

    var conversion: Double {
        let unitToConvert = Double(checkValue) ?? 0

        switch (initialTemperatureUnit, finalTemperatureUnit) {
            case (0,1):
                // c -> f
                return unitToConvert * 1.8 + 32
            case (0,2):
                // c -> k
                return unitToConvert + 273.15
            case (1,0):
                // f -> c
                return (unitToConvert - 32) * 1.8
            case (1,2):
                // f -> k
                return (unitToConvert - 32) * 1.8 + 273.15
            case (2,0):
                // k -> c
                return unitToConvert - 273.15
            case (2,1):
                // k -> f
                return (unitToConvert - 273.15) * 1.8 + 32
            default:
                return unitToConvert
        }
    }

    var body: some View {
        Text("Your View here...a picker and a text field to enter an initial value with a binding to $checkvalue")
    }
}

Dan

5      

I used convert to lowest Temp (k) then did calculation from that

    let tempUnits = ["Celsius", "Fahrenheit", "Kelvin"]

    var convertedTotal: Double {
        let temp = Double(originalNumber) ?? 0
        var kTemp = 0.0
        var finalTemp = 0.0

        switch unitTypeFrom {
        case 0:
            kTemp = temp + 273.15
        case 1:
            kTemp = (temp + 459.67) * 5 / 9
        default:
            kTemp = temp
        }

        switch unitTypeTo {
        case 0:
            finalTemp = kTemp - 273.15
        case 1:
            finalTemp = 1.8 * (kTemp - 273.15) + 32
        default:
            finalTemp = kTemp
        }

        return finalTemp
    }

then used a Picker to get the case number one for unitTypeFrom and one for unitTypeTo and a Text to show finalTemp

Good Luck

4      

@NigelGee + @GeoMod Many thanks

"You can use a switch statement to switch on multiple items. " Very useful!!

Cuicaboy

3      

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!

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.