Skip to content

Instantly share code, notes, and snippets.

@berikv
Created January 13, 2022 16:19
Show Gist options
  • Save berikv/be89eca65dadedbd1137c5a9ca743535 to your computer and use it in GitHub Desktop.
Save berikv/be89eca65dadedbd1137c5a9ca743535 to your computer and use it in GitHub Desktop.
Allow views to print values in ViewBuilders
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