Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple printMessage extension to SwiftUI's View struct
// from the blog post: https://geekanddad.wordpress.com/2020/01/23/snippet-swiftui-view-printmessage-extension/
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
TestView(label: "hello", idx: 22)
.font(.title)
TestView(label: "hello", idx: 24)
.foregroundColor(Color.red)
Spacer()
}
}
}
struct TestView: View {
let label: String
let idx: Int
var body: some View {
Text(label)
.padding(10)
.printMessage("\(self.label):", "\(self.idx)")
// or .printMessage("self.label, self.idx) // output: hello 22
}
}
extension View {
func printMessage(_ msg: Any...,
separator: String = " ",
terminator: String = "\n")
-> some View {
// Print them out as if not
// converted to an array.
for m in msg {
print(m,
separator,
terminator: "")
}
print()
return self
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
hello: 22
hello: 24
hello: 22
hello: 24
hello: 22
hello: 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.