Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created October 21, 2020 11:52
Show Gist options
  • Save pookjw/f4a654e6a259e5885ea5d7cf78f5ab6c to your computer and use it in GitHub Desktop.
Save pookjw/f4a654e6a259e5885ea5d7cf78f5ab6c to your computer and use it in GitHub Desktop.
Failable Initializer for SwiftUI Text...
import SwiftUI
extension Text {
init?<T>(_ content: T?) where T: StringProtocol {
guard let wrapped = content else { return nil }
self = Self.init(wrapped)
}
}
// Example
let nilStr: String? = nil
let str: String = "Hello World!"
Text(nilStr) // nil
Text(str) // non-nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment