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

SOLVED: Day 19 Challenge

Forums > 100 Days of SwiftUI

Hi everyone, i was able to do this challenge. Then i went to see Paul's tips to see if i was missing something and tried to add .formatted() on my output result, but it gives me an undisclosed error, saying it might be a bug.

My code is this: With .formatted() that's giving me problems included:

import SwiftUI

struct ContentView: View {

    @State private var inputValue = "sec"
    @State private var outputValue = "min"
    @State private var userInput: Double = 60

    @FocusState private var isFocused: Bool

   private let values = ["sec", "min", "hours", "days"]

    private var inputConverted: Double {

        let finalInput: Double = userInput

        switch inputValue {
        case "sec":
            return finalInput
        case "min":
            return finalInput * 60
        case "hours":
            return finalInput * 3600
        case "days":
            return finalInput * 86400
        default:
            return finalInput
        }
    }

   private var outputConverted: Double {

        let finalOutput: Double = inputConverted

        switch outputValue {
        case "sec":
            return finalOutput
        case "min":
            return finalOutput / 60
        case "hours":
            return finalOutput / 3600
        case "days":
            return finalOutput / 86400
        default:
            return finalOutput
        }

    }

    var body: some View {
        NavigationView{
            Form{
                Section{
                    Picker("Input value", selection: $inputValue) {
                        ForEach(values, id: \.self){
                            Text("\($0)")
                        }
                    }
                    .pickerStyle(.segmented)
                }header: {
                    Text("Select Input")
                }

                Section{
                    Picker("Output value", selection: $outputValue) {
                        ForEach(values, id: \.self){
                            Text("\($0)")
                        }
                    }
                    .pickerStyle(.segmented)
                }header: {
                    Text("Select Output")
                }

                Section {
                    TextField("Insert input value", value: $userInput, format: .number)
                        .keyboardType(.numberPad)
                        .focused($isFocused)
                }header: {
                    Text("Insert input value")
                }

                Section {
                    Text(outputConverted.formatted(), format: .number)
                }header: {
                    Text("Result")
                }
            }
            .navigationTitle("Time Converter")
            .toolbar {
                ToolbarItemGroup(placement: .keyboard){
                    Spacer()
                    Button("Done"){
                        isFocused = false
                    }
                }
            }
        }
    }
}

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

2      

Hi! Just update this line. I.e. remove formatted()

 Text(outputConverted, format: .number)

2      

That was what i had previously before watching the .formatted() tip. Does it mean that format: .number does the same thing?

2      

Absolutely so. You pass it number it turns it into text representation. As documentation says: init(_:format:) creates a text view that displays the formatted representation of a nonstring type supported by a corresponding format style. And func formatted() -> String returned you a string. So init on Text cannot do that already as you give it a text instead of a number and it says you no way :)

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.

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.