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

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.