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

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

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.