Just a simple utility to print a string in the middle of a view chain when doing SwiftUI (pardon narrow formatting for blog):
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 } }
Use like this:
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) } } PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
// In a Swift playground the output is: hello: 22 hello: 24 hello: 22 hello: 24 hello: 22 hello: 24
Gist here.