Last active
January 23, 2020 19:17
-
-
Save GeekAndDad/e84df42136066cb5b043eb0e9c82759d to your computer and use it in GitHub Desktop.
Simple printMessage extension to SwiftUI's View struct
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
// 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()) |
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
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