0

I have just decided I want to make a watch app and am trying to familiarize myself with Xcode and Swift. So I decided to follow the Workout app for watchOs tutorial and the tutorial used this block of code.

Text(
     Measurement(
      value: 47,
      unit: UnitEnergy.kilocalories
     ).formatted(
       .measurement(
        width: .abbreviated,
        usage: .workout,
        numberFormat: .numeric(
         precision: .fractionLength(0)
        )
       )
      )
    )

However when I try to use this code I get the following error.

Value of type 'Measurement<UnitEnergy>' has no member 'formatted'

Im using XCode Version 12.5.1

Any help is appreciated!

Epicminetime
  • 183
  • 1
  • 1
  • 6
  • You need Xcode 13 beta (SwiftUI 3) for this. Or use a [MeasurementFormatter](https://developer.apple.com/documentation/foundation/measurementformatter) – Joakim Danielson Aug 19 '21 at 06:34

1 Answers1

1

This is the new way style which is required iOS 15.0, Xcode 13.0+ and watchOS 8.0+. Check here for more

For the lower version, you can use it like this

struct ContentView: View {
    
    static let var formatter: MeasurementFormatter = {
        let formatter = MeasurementFormatter()
        formatter.unitStyle = .long
        formatter.unitOptions = .providedUnit
        return formatter
    }()
    
    var body: some View {
        Text("\(Measurement(value: 47, unit: UnitEnergy.kilocalories), formatter: Self.formatter)")
    }
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52