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

Day 19 My Solution

Forums > 100 Days of SwiftUI

//
//  ContentView.swift
//  testday19
//
//  Created by Davide Marzorati on 04/09/22.
//

//Length conversion: users choose meters, kilometers, feet, yards, or miles.
import SwiftUI

struct ContentView: View {

    @State private var inputNumberlenght = 1.0
    @State private var typeLenghtInput = "meters"
    @State private var typeLenghtOutput = "meters"

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

    //creo una variabile computata che trasforma tutto in metri
    var calcolo : Double {

        var variabile : Double = 0.0
        var variabile2 : Double = 0.0

        //converto tutto in metri
        if typeLenghtInput == lenghtType[0] {
            variabile = 1.0
        } else if typeLenghtInput == lenghtType[1] {
            variabile = 0.001
        } else if typeLenghtInput == lenghtType[2] {
            variabile = 0.304
        } else if typeLenghtInput == lenghtType[3] {
            variabile = 0.914
        } else {
            variabile = 1609.34
        }

        let misuraInMetri  = inputNumberlenght / variabile

        //trasformo quello che ho in metri in quello che vuole sapere l'utente

        if typeLenghtOutput == lenghtType[0] {
            variabile2 = 1.0
        } else if typeLenghtOutput == lenghtType[1] {
            variabile2 = 0.001
        } else if typeLenghtOutput == lenghtType[2] {
            variabile2 = 0.304
        } else if typeLenghtOutput == lenghtType[3] {
            variabile2 = 0.914
        } else {
            variabile2 = 1609.34
        }

        let misuradefinitiva = misuraInMetri * variabile2

        return misuradefinitiva
    }

    var body: some View {

        NavigationView {
        Section {
            Form {
                Section{

                    TextField("Enter your number", value: $inputNumberlenght, format: .number)

                }

                // che unità è

                Section {
                   // Text("sceglie la percentuale")
                    Picker("percentuale", selection: $typeLenghtInput) {

                        ForEach(lenghtType, id: \.self) {
                            Text($0)
                        }
                    }.pickerStyle(.segmented)
                }
            header: {
                Text("scegli il tipo di lunghezza")
            }

                // in che unità lo vuoi trasformare
                Section {
                   // Text("sceglie la percentuale")
                    Picker("percentuale", selection: $typeLenghtOutput) {

                        ForEach(lenghtType, id: \.self) {
                            Text($0)
                        }
                    }.pickerStyle(.segmented)
                }
            header: {
                Text("scegli in cosa trasformarlo")
            }

                // restituisci il risultto

                Section {

                    Text(calcolo, format: .number)
                } header: {
                    Text("risultato")
                }

            }

        }
        .navigationTitle("trasforma le misure")

       //

        }

    }
}

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

2      

Questo è un ottimo codice per il giorno 19. Ma in inglese lo scriviamo: length ! -5 punti su Davide. 😉

Also, considera di evitare nomi di variabili generici: variabile o variabile2. Nel tuo programma cosa rappresentano? Questi sono fattore di trasformazione? Ergo: fattoreTrasformazione

Bene!

Continua a codificare

3      

Grazie del feedback!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.