GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Day 19: Solved : **I would like someone to grade my code or let me know if i have made any mistakes? Why my simulator shows two done while building this code.

Forums > SwiftUI

import SwiftUI

struct ContentView: View {

@State private var inputUnit = "Inch"
@State private var outputUnit = "Inch"
@State private var userInputNumber = 0.0

@FocusState private var inputIsFocused: Bool

let units = ["Inch","Feet","Meter","KM"]

var inputConverted : Double {
    let finalInput: Double = userInputNumber
    switch inputUnit {
    case "Inch":
        return finalInput
    case "Feet":
        return finalInput * 12
    case "Meter":
        return finalInput * 39.3701
    case "KM":
        return finalInput * 39370.1
    default:
        return finalInput
    }
}

var outputConverted: Double {
    let finalOutput: Double = inputConverted
    switch outputUnit {
    case "Inch":
        return finalOutput
    case "Feet":
        return finalOutput / 12
    case "Meter":
        return finalOutput / 39.3701
    case "KM":
        return finalOutput / 39370.1
    default:
        return finalOutput
    }
}

var body: some View {
    NavigationStack {
        Form {
            Section("Select Input Unit"){
                Picker("Pick Unit", selection: $inputUnit){
                    ForEach(units, id: \.self){
                        Text($0)
                    }
                }
                .pickerStyle(.segmented)
            }
            Section("Select output Unit"){
                Picker("Pick Unit", selection: $outputUnit){
                    ForEach(units, id: \.self){
                        Text($0)
                    }
                }
                .pickerStyle(.segmented)
            }
            Section("Insert value") {
                TextField("User Input", value: $userInputNumber, format: .number)
                    .keyboardType(.decimalPad)
                    .focused($inputIsFocused)
            }

            Section("output value") {
                Text(outputConverted, format: .number)
            }
            .navigationTitle("ConverLite")
            .toolbar {
                if inputIsFocused {
                    Button("Done") {
                        inputIsFocused = false
                    }
                }
            }
        }
    }
}

}

Preview {

ContentView()

}

   

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.