10
import Foundation
import SwiftUI

struct SampleComponent: View {
  @Binding var value: Decimal

  var body: some View {
    return Text("\(value)")
  }
}

Gives me the error:

Instance method 'appendInterpolation' requires that 'Decimal' conform to '_FormatSpecifiable'

This works fine in a playground though:

import Foundation

var d: Decimal = 4.5
print("\(d)")

Any idea what's going on or how to fix it?

Senseful
  • 86,719
  • 67
  • 308
  • 465

1 Answers1

25

Not exactly sure why it gives this error, but one solution is to cast it to a String:

Text("\(value)" as String)
Senseful
  • 86,719
  • 67
  • 308
  • 465
  • 2
    Man, I've been looking for a solution to this generics problem for a while now... I tried a lot of protocol conformance and other combinations, but it never worked. I'd still love to understand why doing this force-cast fixes the issue though... – Romain Apr 24 '21 at 21:11
  • 4
    I had a similar problem trying to cast a value that isn't a string. The as String method did not work for me, however this did: Text("\(String(describing: value)") – Jokz May 23 '21 at 14:26