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

Day 19 Chellenge solution: convert units using Unit and Measurement

Forums > 100 Days of SwiftUI

@mhak  

//
//  ContentView.swift
//  Chellenge1-Converto
//
//  Created by Mohamed Abdelhak on 19.02.21.
//

import SwiftUI

struct ContentView: View {

    @State private var value = ""
    @State private var unitIn = 0
    @State private var unitOut = 0

    let units = ["seconds", "minutes", "hours"]
    let unitsDuration: [UnitDuration] = [.seconds, .minutes, .hours]

    var result: Double {

        let userValue = Double(value) ?? 0
        let unitToConvert = Measurement.init(value: userValue, unit: unitsDuration[unitIn])
        let unitIsConverted = unitToConvert.converted(to: unitsDuration[unitOut])
        return unitIsConverted.value

    }

    var body: some View {
        NavigationView {
            Form {
                Section {

                    TextField("Enter value", text: $value)
                        .font(.largeTitle)
                        .keyboardType(.decimalPad)
                        .padding(.all)
                    Picker("Unit", selection: $unitIn) {
                        ForEach(0 ..< units.count) {
                            Text("\(units[$0])")
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }

                Section {
                    HStack {
                        Spacer()
                        Text("=")
                            .font(.largeTitle)
                            .fontWeight(.medium)
                        Spacer()
                    }
                }

                Section {
                    Picker("Unit", selection: $unitOut) {
                        ForEach(0 ..< units.count) {
                            Text("\(units[$0])")
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                    Text("\(result, specifier: "%.2f") \(units[unitOut])")
                        .font(.largeTitle)
                        .keyboardType(.decimalPad)
                        .padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
                }

            }
        }
    }

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

https://monosnap.com/file/mppAi1T9OD5kVoEq3bpRY1GdX2SB8D

2      

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.