title: Updating SwiftUI view from an Objective C Class
body: <p>I am not experienced with IOS development but have some basic understanding to work my way through it by reading docs and tutorials.</p>
  <p>I wanted to call Objective C code from Swift and it worked fine, now I want to do the opposite and getting confused a bit.</p>
  <p>Basically I first call an Objective C function in the action of a Button in SwiftUI, then I want that function to update an ObservedObject in the same SwiftUI view and want the view to re-render.</p>
  <p>I have found and followed a few resources on that, which are</p>
  <p><a href="https://medium.com/@iainbarclay/adding-swiftui-to-objective-c-apps-63abc3b26c33" rel="nofollow noreferrer">https://medium.com/@iainbarclay/adding-swiftui-to-objective-c-apps-63abc3b26c33</a></p>
  <p><a href="https://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/" rel="nofollow noreferrer">https://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/</a></p>
  <p>Swift UI view looks like</p>
  <pre><code>class Foo : ObservableObject {
      @Published var bar = &quot;&quot;
  }
  
  struct ContentView: View {
      @ObservedObject var baz = Foo();
      
      // Then access later as self.baz.bar as a parameter somewhere..
  
  </code></pre>
  <p>What would be the right way to update <code>bar</code> here ?</p>
  <p>I did the correct build settings and added <code>@objc</code> tags and also imported <code>project_name-swift.h</code>.
  Implemented and modified the example in
  <a href="https://medium.com/@iainbarclay/adding-swiftui-to-objective-c-apps-63abc3b26c33" rel="nofollow noreferrer">https://medium.com/@iainbarclay/adding-swiftui-to-objective-c-apps-63abc3b26c33</a> but got lost a bit because of my lack of experience in these environments.</p>
  <p>Maybe somebody can push me in the right direction.</p>
  <p>Thank you.</p>
  <p>Let's assume my project name is <code>Project</code>.</p>
  <p>Example code :
  (A code very similar to this, compiles fine and the Objective C function calls, but on the swift side I get no output to console and the text doesn't render. I would really appreciate if you point my mistakes in this, since I get very rarely involved in iOS development.)</p>
  <p>ContentView.swift</p>
  <pre><code>import Foundation
  import SwiftUI
  
  var objectivec_class = Objectivec_Class()
  
  class Foo : ObservableObject {
      @Published var bar = &quot;&quot;
  }
  
  @objc
  class BridgingClass: NSObject {
  
      @ObservedObject var baz = Foo();
      @objc func updateString(_ content: NSMutableString) {
          print(&quot;This function is called from Objective C&quot;)
          self.baz.bar += content as String
      }
  
  }
  
  struct ContentView: View {
      /** 
       * This part seems fishy to me, 
       * It would have been better to inject the instance of Foo here in 
       * BridgingClass but, couldn't figure out how to.
       * This is only for showing my intention. 
       */
      @ObservedObject var baz = Foo();
      var body: some View {
          Button(action: {
              objectivec_class.updateSwiftUi()
          })
          {
              Text(self.baz.bar)
          }
      }
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  
  </code></pre>
  <p>Objective C Bridging Header,</p>
  <p>Project-Bridging-Header.h</p>
  <pre><code>#import &quot;Objectivec_Class.h&quot;
  
  </code></pre>
  <p>Objectivec_Class.h</p>
  <pre><code>#ifndef Objectivec_Class_h
  #define Objectivec_Class_h
  
  #import &lt;Foundation/Foundation.h&gt;
  #import &quot;Project-Swift.h&quot;
  
  @interface Objectivec_Class : NSObject
  
      @property (strong, nonatomic) NSMutableString* stringWhichWillBeRendered;
      @property  BridgingClass *bridgingClass;
  
      - (id) init;
  
      - (void) updateSwiftUi;
  
  @end
  
  #endif /* Objectivec_Class_h */
  
  </code></pre>
  <p>Objectivec_Class.m</p>
  <pre><code>
  #import &lt;Foundation/Foundation.h&gt;
  #import &quot;Project-Swift.h&quot;
  #import &quot;Objectivec_Class.h&quot;
  
  @implementation Objectivec_Class
  
      - (id)init{
          if( self = [super init] ){
              _stringWhichWillBeRendered  = [NSMutableString stringWithString:@&quot;&quot;];
              BridgingClass *bridgingClass = [BridgingClass new];
          }
          return self;
      }
  
      - (void) updateSwiftUi {
          NSString *thisWillBeRendered = @&quot;Render this string.&quot;;
          [_stringWhichWillBeRendered appendString:thisWillBeRendered];
          [[self bridgingClass] updateString:_stringWhichWillBeRendered];
      }
  
  @end
  
  
  </code></pre>