GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

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.