Video wird geladen...

Video konnte nicht geladen werden

Zur Startseite

``` let viewModel = ViewModel(arg) _viewModel = StateObject(wrappedValue: viewModel) ``` Have you ever initialized a StateObject like this? 🤨 As it turns out, this code is actually incorrect and contains a very subtle bug 😱 Let me explain 😌

17,116 Aufrufe • vor 1 Jahr •via X (Twitter)

10 Kommentare

Profilbild von Cocoanetics 
Cocoanetics vor 1 Jahr

OMG I think I have this exact mistake in multiple places!!! Thank you for this!

Profilbild von Vincent Pradeilles
Vincent Pradeillesvor 1 Jahr

It’s such a tricky one 🫠

Profilbild von Sau
Sauvor 1 Jahr

Good catch!! I need to fix a couple of them 😂🔥🔥

Profilbild von 1Dante
1Dantevor 1 Jahr

Wow, I always do as in example, thanks for clarifying this potential issue.

Profilbild von Michael Schwab
Michael Schwabvor 1 Jahr

Great content. It happens to be what I’m grappling with right now.

Profilbild von Jakob Joseph
Jakob Josephvor 1 Jahr

Init it in .task instead :)

Profilbild von Vincent Pradeilles
Vincent Pradeillesvor 1 Jahr

That's also a possibility, but it does require making the values optional, right?

Profilbild von Nicolai Schneider
Nicolai Schneidervor 1 Jahr

Thank you, I’m sure I’ve made this mistake here and there before!

Profilbild von José Gutiérrez
José Gutiérrezvor 1 Jahr

Do you have the link for the article that you mention in the end?

Profilbild von Dong Nguyen
Dong Nguyenvor 1 Jahr

I like simplicity, so I will create a static variable in a class that I call ‘Memory’ :D I don’t like viewModel with constructors that have additional parameters.

Ähnliche Videos