如何对泛型类型进行字符串插值

Ser*_*ost 3 swiftui

希望传递一个Value可以用字符串表示的泛型类型Text("\(value)"

现在,从字面上看,唯一的两种可能的数据类型是Stringand Double(或其他一些数字类型),所以也许我在这里走错了路,所以我愿意寻求最佳路径的建议。

这是我正在尝试做的一个例子..

struct AnswerView<Value: ExpressibleByStringLiteral>: View {
   let value: Value
   var body: some View {
       Text("Your answer is \(value)") //<--Error: No exact matches in call to instance method 'appendInterpolation'
   }
}
Run Code Online (Sandbox Code Playgroud)

jn_*_*pdx 6

使用String(describing:)

Text("Your answer is \(String(describing: value))")
Run Code Online (Sandbox Code Playgroud)

或(非常相似):

struct AnswerView<Value: CustomStringConvertible>: View {
   let value: Value
   var body: some View {
    Text("Your answer is \(value.description)")
   }
}
Run Code Online (Sandbox Code Playgroud)