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

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

На главную

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

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

Фото профиля Cocoanetics 
Cocoanetics 2 лет назад

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

Фото профиля Vincent Pradeilles
Vincent Pradeilles2 лет назад

It’s such a tricky one 🫠

Фото профиля Sau
Sau2 лет назад

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

Фото профиля 1Dante
1Dante2 лет назад

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

Фото профиля Michael Schwab
Michael Schwab2 лет назад

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

Фото профиля Jakob Joseph
Jakob Joseph2 лет назад

Init it in .task instead :)

Фото профиля Vincent Pradeilles
Vincent Pradeilles2 лет назад

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

Фото профиля Nicolai Schneider
Nicolai Schneider2 лет назад

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

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

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

Фото профиля Dong Nguyen
Dong Nguyen2 лет назад

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.

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