title: Is there a way to call a function when a SwiftUI Picker selection changes?
body: <p>I would like to call a function when selectedOption's value changes. Is there a way to do this in SwiftUI similar to when editing a TextField?</p>
  
  <p>Specifically, I would like to save the selected option when the user changes the selectedOption.</p>
  
  <p>Here is my picker:</p>
  
  <pre><code>struct BuilderPicker: View {
      let name: String
      let options: Array&lt;String&gt;
      @State var selectedOption = 0
      var body: some View {
          HStack {
              Text(name)
                  .font(.body)
                  .padding(.leading, 10)
              Picker(selection: $selectedOption, label: Text(name)) {
                  ForEach(0 ..&lt; options.count) {
                      Text(self.options[$0]).tag($0)
                  }
              }.pickerStyle(SegmentedPickerStyle())
                  .padding(.trailing, 25)
          }.onTapGesture {
              self.selectedOption = self.selectedOption == 0 ? 1 : 0
          }
              .padding(.init(top: 10, leading: 10, bottom: 10, trailing: 0))
              .border(Color.secondary, width: 3)
              .padding(.init(top: 0, leading: 15, bottom: 0, trailing: 15))
              .font(.body)
      }
  
  }
  </code></pre>
  
  <p>I’m still new to SwiftUI and would love some help. Thanks!</p>