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

SOLVED: Day 19: Challenge (Measurement)

Forums > 100 Days of SwiftUI

I started the project and I made this so far using Measurement<Unit>, but I don't not why my formated input Text shows the output. When I first run the app, the calculation seems to work, but when I change the units, it shows wrong answers (strange behaviour). Here is my code, I appreciate so much any help or guidence!

enum Temperature: String, Equatable, CaseIterable {
  case Celsius, Fahrenheit, Kelvin
}

struct ContentView: View {
  @State private var inputValue: Double = 0
  @State private var inputUnit: Temperature = .Celsius
  @State private var outputUnit: Temperature = .Fahrenheit
  @FocusState private var inputIsFocused: Bool

  var inputMeasurement: Measurement<UnitTemperature> {
    switch inputUnit {
    case .Celsius:
      return Measurement(value: inputValue, unit: UnitTemperature.celsius)
    case .Fahrenheit:
      return Measurement(value: inputValue, unit: UnitTemperature.fahrenheit)
    case .Kelvin:
      return Measurement(value: inputValue, unit: UnitTemperature.kelvin)
    }
  }

  var outputMeasurement: Measurement<UnitTemperature> {
    let output = inputMeasurement
    switch outputUnit {
    case .Celsius:
      return output.converted(to: .celsius)
    case .Fahrenheit:
      return output.converted(to: .fahrenheit)
    case .Kelvin:
      return output.converted(to: .kelvin)
    }
  }

  var body: some View {
    NavigationView {
      Form {
        Section {
          Picker("Input Unit", selection: $inputUnit) {
            ForEach(Temperature.allCases, id: \.self) {
              Text($0.rawValue)
            }
          }
          .pickerStyle(.automatic)

          TextField("Enter the amount", value: $inputValue, format: .number)
            .keyboardType(.decimalPad)
            .focused($inputIsFocused)
          Text(inputMeasurement.formatted())
        } header: {
          Text("Input Parameters")
        }

        Section {
          Picker("Output Unit", selection: $outputUnit) {
            ForEach(Temperature.allCases, id: \.self) {
              Text($0.rawValue)
            }
          }
          .pickerStyle(.automatic)

          Text(outputMeasurement.formatted())
        } header: {
          Text("Output Parameters")
        }
      }
      .navigationTitle("Unit Converter")
      .toolbar {
        ToolbarItemGroup(placement: .keyboard) {
          Spacer()
          Button("Done") {
            inputIsFocused = false
          }
        }
      }
    }
  }
}

2      

It is the .formatted() part that is the issue here.

Text(outputMeasurement.formatted())

As you never specify the MeasurementFormatter options, it defaults to your locale settings, in your case °C.

There are options for the MeasurementFormatter that you can set. Add the following after the @FocusState line.

let unitsFormat:  MeasurementFormatter

init() {
    unitsFormat = MeasurementFormatter()
    unitsFormat.unitOptions = .providedUnit
}

and replace your outputMeasurement text line with

Text(unitsFormat.string(from: outputMeasurement))

There is more information in the Xcode / Swift documentation. Select MeasurementFormatter and control-click on it, then select the Show Quick Help to see more details and help.

3      

If you want to use the new-style formatters, you can do this:

Text(outputMeasurement.formatted(.measurement(usage: .asProvided)))

3      

Thank you guys! :)

2      

@Bnerd  

Am I the only one that did plain math and if statements? Joke aside, at least I learned something from your approach!

2      

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!

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.