TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 19: Challenge Day

Forums > SwiftUI

HI!

I am totally new with switft and switfui, sand also english is not my first language, so I am going quite slow...

I have just finished my first challenge with proyects...

I think it works properly, but i don´t know if I have done it right or i have missed important things...

Also, in the tips section, it says "Finally, if you’d like to be a bit fancy with the output number you display, try calling .formatted() on it – e.g. someDouble.formatted(). This will cause iOS to format the number for printing, adding thousands separator and removing a lot of the noise from unnecessary decimal places."

I don't understant what it is for and where and how I have to insert it...

Could you please help me?

import SwiftUI

struct ContentView: View {

    let timeUnits = ["seconds", "minutes", "hours", "days"]

    @State private var inputTimeUnit = "seconds"
    @State private var outputTimeUnit = "seconds"
    @State private var enterValue = 0.0

    @FocusState private var valueIsFocused: Bool

    var inputConverted: Double {

        switch inputTimeUnit {
        case "seconds": return enterValue
        case "minutes": return enterValue * 60
        case "hours": return enterValue * 3600
        case "days": return enterValue * 86400.001
        default: return 0
        }
    }

    var outputCoverted: Double {

        switch outputTimeUnit {
        case "seconds": return inputConverted
        case "minutes": return inputConverted / 60
        case "hours": return inputConverted / 3600
        case "days": return inputConverted / 86400.001
        default: return 0
        }
    }

    var body: some View {

        NavigationStack {

            Form {

                Section ("Number to convert") {
                    TextField ("Enter number", value: $enterValue, format: .number)
                        .keyboardType(.numberPad)
                        .focused($valueIsFocused)
                }

                Section ("Input unit") {
                    Picker ("Input Unit", selection: $inputTimeUnit) {
                        ForEach(timeUnits, id: \.self) {
                            Text ($0)
                        }
                    }
                    .pickerStyle(.segmented)
                }

                Section ("Output Unit") {
                    Picker ("Output Unit", selection: $outputTimeUnit) {
                        ForEach(timeUnits, id:\ .self) {
                            Text ($0)
                        }
                    }
                    .pickerStyle(.segmented)
                }

                Section ("Converted number") {
                    Text (outputCoverted, format: .number)

                }
            }
            .navigationTitle("Time conversion")
            .toolbar {
                if valueIsFocused {
                    Button ("Done") {
                        valueIsFocused = false
                    }
                }
            }
        }
    }
}

#Preview {
    ContentView()
}

   

Kitty wonders about modifiers:

I don't understant what .formatted() is for and where and how I have to insert it...

Welcome to Hacking With Swift

Slow is the way to go!

Think of .formatted() as a modifier. It modifies the object it's attached to.

For example, you can think of a shoe as a foot modifier.
A ski boot modifies your foot to attach to a ski.
A high-heel shoe modifies your overall height.
A hiking boot modies your ability to travel over rocky paths.
A sleek running shoe modifies your ability to move quickly.

So what does the .formatted() modifier actually modify?

If you attach a .formatted() modifier to a double value, you can change the way it appears when you print the value.

Here's a short example for you to enter into Xcode's playground. I encourage you to search these forums for Playground for many examples. I also encourage you to keep code examples in playground files, as these will help you down the road!

let currencyAmount = 1042.009

print (currencyAmount)                                     //     1042.009
print (currencyAmount.formatted())                         //    1,042.009

print (currencyAmount.formatted(.currency(code: "EUR")))   //   €1,042.01 (notice rounding up)
print (currencyAmount.formatted(.currency(code: "USD")))   // US$1,042.01
print (currencyAmount.formatted(.currency(code: "JPY")))   // JP¥1,042    (notice no decimal!)

Localisation

You may live in a part of the world where the thousands portion of a number is separated by a period (.) and the decimal portion is separated by a comma (,). This would be difficult to program all the variations for all the world's users.

Instead, allow Swift to make these conversions for you! In your locale, you may see €1.042,01 instead of the format used in UK, USA, and Canada €1,042.01

Keep Coding

   

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.

Learn more 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.