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

SOLVED: Passing .formatted(...) to a View

Forums > SwiftUI

I am trying to pass a .formatted(...) to a view, something like this:

NumberPreviewer(1000, minimizedStyle: .number, maximizedStyle: .percent)

But it's not compiling and I am getting the following error: No exact matches in call to instance method 'formatted' No exact matches in call to instance method 'appendInterpolation'

Here is the code for NumberPreviewer:

struct NumberPreviewer<S>: View where S: FormatStyle {

  private let number: Double?
  @State private var state: PreviewState
  private let minimizedStyle: S
  private let maximizedStyle: S

  init(
    _ number: Double,
    state: PreviewState = .minimized,
    minimizedStyle: S,
    maximizedStyle: S
  ) {
    self.number = number
    self._state = State(wrappedValue: state)
    self.minimizedStyle = minimizedStyle
    self.maximizedStyle = maximizedStyle
  }

  var body: some View {
    if let number {
        switch state {
          case .maximized:
            Text("\(number.formatted(maximizedStyle))") // error is here
          case .minimized:
            Text("\(number.formatted(minimizedStyle))") // error is here
        }
    } else {
      Text("-")
    }
  }

  enum PreviewState {
    case maximized
    case minimized

    mutating func toggle() {
      switch self {
        case .maximized:
          self = .minimized
        case .minimized:
          self = .maximized
      }
    }
  }
}

2      

Hi,

You need to explicitly state the FormatOutput equal to String also you need 2 generics since S can't be .number and .percent at the same time.

struct NumberPreviewer<S, T>: View where S : FormatStyle, T : FormatStyle, S.FormatInput == Double, T.FormatInput == Double, S.FormatOutput == String, T.FormatOutput == String {

    private let number: Double?
    @State private var state: PreviewState
    private let minimizedStyle: S
    private let maximizedStyle: T

      init(
        _ number: Double,
        state: PreviewState = .minimized,
        minimizedStyle: S,
        maximizedStyle: T
      ) {
        self.number = number
        self._state = State(wrappedValue: state)
        self.minimizedStyle = minimizedStyle
        self.maximizedStyle = maximizedStyle
      }

          var body: some View {
              if let number {
                      switch state {
                        case .maximized:
                          Text("\(maximizedStyle.format(number))")
                        case .minimized:
                          Text("\(minimizedStyle.format(number))")
                      }
                  } else {
                    Text("-")
                  }
          }

            enum PreviewState {
                case maximized
                case minimized

            mutating func toggle() {
            switch self {
                case .maximized:
                  self = .minimized
                case .minimized:
                  self = .maximized
            }
        }
    }
}

2      

Thanks a lot.

2      

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.