title: How to have a dependency set as a @propertyWrapper be a @ObservableObject?
body: <p>I have a first @property wrapper that is made to be UserDefaults called <code>@UserDefault(key: .isSignedIn, defaultValue: false) var isSignedIn: Bool</code>. it is working fine as a publisher by using it this way, <code>$isSignedIn</code> to update my SwiftUI view when used by itself. But I would like to make it in a dependency set in a view model.</p>
  <p>Now, to hold UserDefaults values, I create a dependency set as a @propertyWrapper called <code>@Injection(\.localDataManager) var localDataManager</code> that will also be used in unit tests using a subscript solution associated with key called <code>InjectedDependency[\.localDataManager] = LocalDataManagerMock()</code></p>
  <p>The problem is that <code>LocalDataManager</code> class is not a <code>@ObservableObject</code> and does not update my SwiftUI view over time.</p>
  <ul>
  <li>How can <code>LocalDataManager</code> be an <code>@ObservableObject</code> using this <code>InjectedDependency</code> unit testing solution?</li>
  </ul>
  <p><strong>The SwiftUI view with the view model:</strong></p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
  
    @StateObject var viewModel = DataViewModel()
  
    var body: some View {
      VStack(spacing: 16) {
        Text(viewModel.localDataManager.isSignedIn.description)
  
        Button(action: { viewModel.localDataManager.isSignedIn.toggle() }) {
          Text(&quot;Toggle boolean button&quot;)
        }
      }
    }
  }
  
  class DataViewModel: ObservableObject {
  
    @Injection(\.localDataManager) var localDataManager
  }
  </code></pre>
  <p><strong>What I would like to use as a <code>@ObservableObject</code>:</strong></p>
  <pre><code>import Foundation
  
  protocol LocalDataManagerProtocol {
    var isSignedIn: Bool { get set }
  }
  
  final class LocalDataManager: LocalDataManagerProtocol {
  
    @UserDefault(key: .isSignedIn, defaultValue: false)
    var isSignedIn: Bool
  }
  
  @propertyWrapper
  struct Injection&lt;T&gt; {
  
    private let keyPath: WritableKeyPath&lt;InjectedDependency, T&gt;
  
    var wrappedValue: T {
      get { InjectedDependency[keyPath] }
      set { InjectedDependency[keyPath] = newValue }
    }
  
    init(_ keyPath: WritableKeyPath&lt;InjectedDependency, T&gt;) {
      self.keyPath = keyPath
    }
  }
  </code></pre>
  <p><strong>The <code>@UserDefault</code> code:</strong></p>
  <pre><code>import Combine
  import SwiftUI
  
  enum UserDefaultsKey: String {
    case isSignedIn
  }
  
  protocol UserDefaultsProtocol {
    func object(forKey defaultName: String) -&gt; Any?
    func set(_ value: Any?, forKey defaultName: String)
  }
  extension UserDefaults: UserDefaultsProtocol {}
  
  @propertyWrapper
  struct UserDefault&lt;Value&gt; {
  
    let key: UserDefaultsKey
    let defaultValue: Value
    var container: UserDefaultsProtocol = UserDefaults.standard
  
    private let publisher = PassthroughSubject&lt;Value, Never&gt;()
  
    var wrappedValue: Value {
      get {
        return container.object(forKey: key.rawValue) as? Value ?? defaultValue
      }
      set {
        container.set(newValue, forKey: key.rawValue)
        publisher.send(newValue)
      }
    }
  
    var projectedValue: AnyPublisher&lt;Value, Never&gt; {
      publisher.eraseToAnyPublisher()
    }
  }
  </code></pre>
  <p><strong>The <code>InjectedDependency</code> solution for Unit Testing:</strong></p>
  <pre><code>// Use in Unit Test code in SetUp()
  // `InjectedDependency[\.localDataManager] = LocalDataManagerMock()`
  
  protocol InjectedKeyProtocol {
    associatedtype Value
    static var currentValue: Self.Value { get set }
  }
  
  struct InjectedDependency {
  
  private static var current = InjectedDependency()
  
  static subscript&lt;K&gt;(key: K.Type) -&gt; K.Value where K: InjectedKeyProtocol {
      get { key.currentValue }
      set { key.currentValue = newValue }
    }
  
    static subscript&lt;T&gt;(_ keyPath: WritableKeyPath&lt;InjectedDependency, T&gt;) -&gt; T {
      get { current[keyPath: keyPath] }
      set { current[keyPath: keyPath] = newValue }
    }
  
    var localDataManager: LocalDataManagerProtocol {
      get { Self[LocalDataManagerKey.self] }
      set { Self[LocalDataManagerKey.self] = newValue }
    }
  }
  
  private struct LocalDataManagerKey: InjectedKeyProtocol {
    static var currentValue: LocalDataManagerProtocol = LocalDataManager()
  }
  </code></pre>