Created
January 13, 2022 16:19
-
-
Save berikv/be89eca65dadedbd1137c5a9ca743535 to your computer and use it in GitHub Desktop.
Allow views to print values in ViewBuilders
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 | |
/** | |
* An @ViewBuilder will only allow statements that result in a View. | |
* `print(value)` does not result in a view and is not allowed in a @ViewBuilder such as `var body: some View`. | |
* This view extension allows you print inside a @ViewBuilder. | |
* | |
* Usage: | |
* ``` | |
* struct MyView: View { | |
* var body: some View { | |
* Text("") | |
* .print("Hello world") | |
* } | |
* } | |
*/ | |
extension View { | |
@discardableResult | |
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") -> Self { | |
Swift.print(items, separator: separator, terminator: terminator) | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment