🌅 Unwrap
可以解開一個 Optional 的值,並將此值轉換成一個 View (Optional<Content>)。
定義
struct Unwrap<Value, Content: View>: View語法
Unwrap(value, then: { /* turn value into content view */ })
Unwrap(value) { /* turn value into content view */ }let value: Int? = 1
let view = Unwrap(value) {
Text("\($0)") // Content == Text
}
let T = type(of: view) // Unwrap<Int, Text>.Type
print(T) // Unwrap<Int, Text>
print(T.Body.self) // Optional<Text>
// ⭐️ use `T.self` to reference the type object itself
T.Body.self == Text?.self // true程式碼

Last updated