Загрузка видео...

Не удалось загрузить видео

На главную

``` 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 просмотров • 1 год назад •via X (Twitter)

Комментарии: 10

Фото профиля Cocoanetics 
Cocoanetics 1 год назад

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

Фото профиля Vincent Pradeilles
Vincent Pradeilles1 год назад

It’s such a tricky one 🫠

Фото профиля Sau
Sau1 год назад

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

Фото профиля 1Dante
1Dante1 год назад

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

Фото профиля Michael Schwab
Michael Schwab1 год назад

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

Фото профиля Jakob Joseph
Jakob Joseph1 год назад

Init it in .task instead :)

Фото профиля Vincent Pradeilles
Vincent Pradeilles1 год назад

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

Фото профиля Nicolai Schneider
Nicolai Schneider1 год назад

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

Фото профиля José Gutiérrez
José Gutiérrez1 год назад

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

Фото профиля Dong Nguyen
Dong Nguyen1 год назад

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.

Похожие видео