Created
October 21, 2020 11:52
-
-
Save pookjw/f4a654e6a259e5885ea5d7cf78f5ab6c to your computer and use it in GitHub Desktop.
Failable Initializer for SwiftUI Text...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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