Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ObservedObject is a get-only property #392

Closed
kuglee opened this issue Mar 20, 2021 · 4 comments
Closed

@ObservedObject is a get-only property #392

kuglee opened this issue Mar 20, 2021 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@kuglee
Copy link

kuglee commented Mar 20, 2021

Describe the bug

   ERROR   cannot assign to property: 'testObservable' is a get-only property
  19 |      self.testObservable = TestObservable()
     |           ^

To Reproduce

import TokamakShim

struct TokamakApp: App {
    var body: some Scene {
        WindowGroup("Tokamak App") {
            ContentView()
        }
    }
}

public class TestObservable: ObservableObject {
  @Published var test = 0
}

public struct ContentView: View {
  @ObservedObject var testObservable: TestObservable
  
  public init() {
    self.testObservable = TestObservable()
  }

  public var body: some View {
    EmptyView()
  }
}

// @main attribute is not supported in SwiftPM apps.
// See https://bugs.swift.org/browse/SR-12683 for more details.
TokamakApp.main()

Expected behavior
The code compiles successfully.

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Safari
  • Version of the browser: 14.0.3 (16610.4.3.1.4)
  • Version of Tokamak: 0.6.1
@kuglee kuglee added the bug Something isn't working label Mar 20, 2021
@foscomputerservices
Copy link
Contributor

Did you try self._testObservable = TestObservable()? It is my understanding that this is how Swift actually stores property wrappers (reference).

@kuglee
Copy link
Author

kuglee commented Mar 20, 2021

Then I get a different error:

   ERROR   cannot assign value of type 'TestObservable' to type 'ObservedObject<TestObservable>'
  20 |      self._testObservable = TestObservable()
     |

@foscomputerservices
Copy link
Contributor

I apologize, I should not have replied from memory. I too have encountered this issue as a difference between the way that iOS works. The following workaround will work:

self._testObservable = ObservedObject(wrappedValue: TestObservable())

@kuglee
Copy link
Author

kuglee commented Mar 20, 2021

Awesome. Thanks!

@kuglee kuglee closed this as completed Mar 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants