title: Swift - How to access @Published var from func outside of view?
body: <p>I'm trying to remove the logic from the view, while keeping the benefits of SwiftUI. Idea 1 works but it makes use of an extra variable than I would want to. Idea 2 gives error: Property wrappers are not yet supported on local properties. The view should return "bar". What is the best way of making this work? Many thanks.</p>
  
  <pre><code>import Combine
  import Foundation
  import SwiftUI
  
  // Model
  
  enum Model: String, RawRepresentable {
  
      case foo = "foo"
      case bar = "bar"
  }
  
  // State
  
  var data1: String = Model.foo.rawValue
  
  class State: ObservableObject {
  
      @Published internal var data2: String = data1
  }
  
  // Logic
  
  func logic() {
  
  // Idea 1: OK
  
      //data1 = Model.bar.rawValue
      //print(State().data2)
  
  // Idea 2: Error Property wrappers are not yet supported on local properties
  
      @EnvironmentObject private var state: State
      state.data2 = Model.bar.rawValue
      print(state.data2)
  }
  
  // View
  
  struct bar: View {
  
      @EnvironmentObject private var state: State
  
      internal var body: some View {
  
          logic()
          return Text(verbatim: self.state.data2)
      }
  }
  </code></pre>