How to print() to Xcode console in SwiftUI?

后端 未结 9 1920
刺人心
刺人心 2020-12-14 00:24

So I tried to put a print statement while debugging in a SwiftUI View.

print(\"landmark: \\(landmark)\")

In the following body.

<         


        
9条回答
  •  无人及你
    2020-12-14 01:02

    Here's a helper Print( ... ) View that acts like a print( ... ) function but within a View

    Put this in any of your view files

    extension View {
        func Print(_ vars: Any...) -> some View {
            for v in vars { print(v) }
            return EmptyView()
        }
    }
    

    and use inside of body like so

    Print("Here I am", varOne, varTwo ...)
    

    or inside a ForEach {} like so

    self.Print("Inside ForEach", varOne, varTwo ...)
    

    Note: you might need to put Print() into a Group {} when combining with existing views

提交回复
热议问题