Print a variable

Can anyone help me? I simply want to print to the console a variable from a picker, where do I place the print statement? I want to print the changing variable in the picker. thank you

@State var selectedWeek: TimeSpan = .two

var body: some View {
    
    NavigationView {
        
        VStack {
            MyMapView()
                .edgesIgnoringSafeArea(.all)
            
            Picker("Time Span", selection: $selectedWeek) {
                ForEach(TimeSpan.allCases) { weekSelected in
                    Text(weekSelected.name).tag(weekSelected)
                }
                
            }.pickerStyle(SegmentedPickerStyle())
                
                .background(Color.gray)
                .padding()
        }.navigationBarTitle(Text("Contact Tracing"), displayMode: .inline)
    }    }

}

You can manually define a binding and print out something there:

var body: some View {
     let binding = Binding<TimeSpan>(get: { self.selectedWeek }, set: { self.selectedWeek = $0; print("Selected: \($0)" })   
    return NavigationView {
        
        VStack {
            MyMapView()
                .edgesIgnoringSafeArea(.all)
            
            Picker("Time Span", selection: binding) {
 
    ... the rest of you code

In preview pane, right-click the run button and choose "Debug Preview"

2 Likes

Thank you I have redesigned and seems to work.