How to print() to Xcode console in SwiftUI?

后端 未结 9 1904
刺人心
刺人心 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 00:47

    At least in Xcode 12/Swift 5.3, you can easily add a print statement anywhere in a function builder by simply storing its return value in a wildcard, effectively ignoring it:

    let _ = print("hi!")
    

    No setup or other verbosity needed!

    0 讨论(0)
  • 2020-12-14 00:51

    You can not print in body structure i.e. a structure which is some view type.For print you need to make function out of body structure and call it using button or something else.

    0 讨论(0)
  • 2020-12-14 01:01

    You can't because you're in a computed property. You need for example a button and in the action you define the print. Or work with breakpoints

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-14 01:05

    Try right-clicking on the live preview play button and selecting 'Debug Preview from the popup

    0 讨论(0)
  • 2020-12-14 01:06

    It is possible to use print() remembering that all SwiftUI View content are (a) implicit closures and (b) it is highly recommended to decompose views as much as possible to have simple structure, so it might look like the following...

    struct Model: Identifiable {
        let value: String
        var id: String {
            value
        }
        init (_ value: String) {
            self.value = value
        }
    }
    
    struct TestView: View {
        @State var showFavoritesOnly = false
        @State var listData: [Model] = [Model("one"), Model("two"), Model("three")]
    
        var body: some View {
            NavigationView {
                List {
                    Toggle(isOn: $showFavoritesOnly) {
                        Text("Favorite only")
                    }
                    ForEach(listData) { data in
                        self.rowView(data: data)
                    }
                }
            }
        }
    
        private func rowView(data: Model) -> some View {
    #if DEBUG
            print(">> \(data.value)")
    #endif
            return NavigationLink(destination: Text("Details")) {
                Text("Go next from \(data.value)")
            }
        }
    }
    

    ... and right clicking in Preview to select run as Debug Preview we get:

    2019-10-31 14:28:03.467635+0200 Test[65344:11155167] [Agent] Received connection, creating agent
    2019-10-31 14:28:04.472314+0200 Test[65344:11155168] [Agent] Received display message
    >> one
    >> two
    >> three
    
    0 讨论(0)
提交回复
热议问题