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

Day 19 Challenge Day - Liquid converter

Forums > 100 Days of SwiftUI

First things first. Paul, you are awesome! I never thought, that I start programming on XCode ever... Love your videos and the way you make things clear. Even as a non-native english speaker, I can follow your descriptions. Here is my solution:

//
//  ContentView.swift
//  Converter Liquids
//
//  Created by Oliver Kleemann on 05.04.22.
//

import SwiftUI

struct ContentView: View {

    @State private var from: String = "Liter"
    @State private var to: String = "Liter"
    @State private var value: Double = 1.0

    // add more units into this dictionary here:
    let units = ["Liter": 1.0,
                 "Milliliter": 1000.0,
                 "Hektoliter": 0.01,
                 "Kubikmeter": 0.001,
                 "US-Gallone": 0.264172,
                 "US-Quart": 1.05669,
                 "US-Pint": 2.11338,
                 "US-Cup": 4.16667,
                 "UK-Gallone": 0.219969,
                 "UK-Quart": 0.879877,
                 "UK-Pint": 1.75975,
                 "UK-Cup": 3.51951]

    // this is the array used for the both pickers
    var unitArray: [String] {
        var outputArray: [String] = []
        for unit in units { outputArray.append(unit.key) }
        return outputArray
    }

    var conversionResult: Double {
        let fromFactor = units[from] ?? 0.0
        let toFactor = units[to] ?? 0.0
        return value / fromFactor * toFactor
    }

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("From unit:", selection: $from) {
                        ForEach (unitArray.sorted(), id: \.self) {
                            Text ($0)
                        }
                        .pickerStyle(.automatic)
                    }

                    TextField ("Value to convert", value: $value, format: .number)
                        .keyboardType(.decimalPad)
                } header: { Text("Input") }

                HStack{
                    Spacer()
                    // exchange the units
                    Button ("↕️"){
                        let exchange = from
                        from = to
                        to = exchange
                    }
                    Spacer()
                }

                Section {
                    Picker("To unit:", selection: $to) {
                        ForEach (unitArray.sorted(), id: \.self) {
                            Text ($0)
                        }
                        .pickerStyle(.automatic)
                    }

                    Text (conversionResult, format: .number)
                } header: { Text ("Result") }
            }
            .navigationTitle("Liquid Converter")
        }
    }
}

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

Took me a long time, but with the knowledge of WeSplit, the goal was reachable. A lot to learn ...

Thanks for your great work!

   

Nice job. Congrats on hanging in there and solving the challenge. In case you want a little less typing, this is a shorter way for creating an array of keys from a dictionary:

var unitArray: [String] { Array(units.keys) }

   

Thanks :) I already had the feeling, that there is a better way to generate this Array. Isn't there maybe a way to go through the keys directly in the ForEach statement? Without the Array?

   

Yes, you can go even futher and remove unitArray; and use this code in your ForEach instead:

ForEach(units.keys.sorted(), id:\.self) {

   

Wonderful! Thank you.

   

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, 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!

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.