-1

I'm trying to format an integer to display it as minutes, but I can't get the syntax right and the docs are still light on this. This is what I'm trying:

let value: Int = 20
Text(value.formatted(.components(style: .abbreviated, fields: [.minute])))
// Compile error: Instance method 'formatted' requires the types 'Int' and 'Date.ComponentsFormatStyle.FormatInput' (aka 'Range<Date>') be equivalent

I'm trying to display: 20 min. How can this be done using the new Foundation formatters improvements from WWDC 2021?

I can do this the old way like this:

static let formatter: DateComponentsFormatter = {
    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .short
    formatter.allowedUnits = [.minute]
    return formatter
}()

Self.formatter.string(for: DateComponents(minute: value))
TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • 1
    The syntax in your code is supposed to be applied to a `Date` related value, approximately this is what the error says. – vadian Jun 15 '21 at 13:38

1 Answers1

0

to format an integer you need a NumberFormatter. Or you could just do:

Text("\(value) min")