WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.