Argument type 'xxx' does not conform to expected type '_FormatSpecifiable'

這看起來是一個問題,但卻產生兩個錯誤,很奇怪。

另一個錯誤訊息:

❌ Cannot convert value of type 'MyClass' to expected argument type 'String'
import SwiftUI

struct MyStruct: CustomStringConvertible {
    var description: String { "hello" }
}

class MyClass: CustomStringConvertible {
    var description: String { "hi" }
}


let a = MyStruct()
let b = MyClass()
print("\(a)")        // hello
print("\(b)")        // hi

// ❌ Argument type 'MyStruct' does not conform to
//    expected type '_FormatSpecifiable'
let text1 = Text("\(a)")

// ❌ Cannot convert value of type 'MyClass' to
//    expected argument type 'String'
let text2 = Text("\(b)")

Last updated