title: Accessing Value of SwiftUI View through a UIKit View
body: <p>I read this question online and found it to be very interesting. If you have a SwiftUI view as shown below. How can you access the selected rating in a UIKit view <em>without</em> changing a single line in RatingView.</p>
  <pre><code>struct RatingView: View {
      
      @Binding var rating: Int?
      
      private func starType(index: Int) -&gt; String {
          
          if let rating = rating {
              return index &lt;= rating ? &quot;star.fill&quot; : &quot;star&quot;
          } else {
              return &quot;star&quot;
          }
      }
      
      var body: some View {
          HStack {
              ForEach(1...5, id: \.self) { index in
                  Image(systemName: self.starType(index: index))
                      .foregroundColor(Color.orange)
                      .onTapGesture {
                          rating = index
                  }
              }
          }
      }
  }
  
  class ViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          self.view.backgroundColor = .white
          
          // .constant(3) will be replaced by something in the ViewController so it can handle the changes for the rating
          let hostingController = UIHostingController(rootView: RatingView(rating: .constant(3)))
          
          guard let ratingsView = hostingController.view else { return }
          self.addChild(hostingController)
          
          self.view.addSubview(ratingsView)
  }
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>Original source: <a href="https://twitter.com/azamsharp/status/1540838477599752192?s=20&amp;t=bbDp3VT9m0Ce4W3Sgr7Iyg" rel="nofollow noreferrer">https://twitter.com/azamsharp/status/1540838477599752192?s=20&amp;t=bbDp3VT9m0Ce4W3Sgr7Iyg</a></p>
  <p>I typed most of the code from the original source. I did not change much.</p>