Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
dTub
Скачать

Using Generic ViewModel Protocols with ObservableObject in SwiftUI MVVM

Автор: vlogommentary

Загружено: 2025-12-13

Просмотров: 2

Описание:

Learn how to properly initialize a SwiftUI View with multiple ObservableObject-conforming ViewModels using generics and protocols for clean MVVM architecture.
---
This video is based on the question https://stackoverflow.com/q/79531038/ asked by the user 'Richard Topchii' ( https://stackoverflow.com/u/3667264/ ) and on the answer https://stackoverflow.com/a/79531070/ provided by the user 'Richard Topchii' ( https://stackoverflow.com/u/3667264/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: SwiftUI MVVM: Single View, 2 different ViewModels that conform to ObservableObject

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.

If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction

In SwiftUI MVVM architecture, you might encounter scenarios where a single View needs to work with multiple different ViewModel classes sharing the same interface. These ViewModels conform to ObservableObject and expose similar properties and methods but differ in behavior or display content.

The question is: How can you initialize a SwiftUI View so that it owns a ViewModel defined by a protocol, while ensuring the ViewModel is an ObservableObject?

The Challenge

You define a protocol, e.g., OverrideViewModelProtocol, with properties and methods your View expects.

Each concrete ViewModel class conforms to this protocol and ObservableObject.

You want your SwiftUI View to hold a @ StateObject of a type conforming to this protocol.

However, protocols with ObservableObject constraints are tricky:

Swift prohibits treating an existential (any OverrideViewModelProtocol) as an ObservableObject directly.

You cannot declare @ StateObject private var viewModel: any OverrideViewModelProtocol because the compiler needs a concrete ObservableObject type.

Common Workarounds

Use an abstract base class conforming to ObservableObject instead of a protocol.

Use type erasure (like AnyObservableObject)—which can be complex and unnecessary here.

Elegant Solution: Use Generics for the ViewModel

SwiftUI Views can be generic over the type of their ViewModel. If you declare your View like this:

[[See Video to Reveal this Text or Code Snippet]]

Why this works:

The generic constraint T: OverrideViewModelProtocol ensures viewModel conforms to the expected protocol.

Since the concrete type T is known at compile time, Swift can handle the @ StateObject correctly.

This avoids using existential types that cannot fulfill ObservableObject requirements.

Usage Example

Assuming you have two different ViewModel classes:

[[See Video to Reveal this Text or Code Snippet]]

Each OverrideView instance owns a concrete ViewModel of a specific type, and your view logic can remain clean, avoiding conditional if/else inside the view.

Summary

Problem: Initializing a SwiftUI View to own a protocol-constrained ObservableObject ViewModel.

Failed approach: Using protocols existentially with @ StateObject.

Best practice: Make the View generic over the ViewModel type constrained by your protocol.

Benefits: Cleaner code, compiler-friendly, and adheres to MVVM separation of concerns.

This pattern simplifies using multiple ViewModels for similar views differing only in content or behavior without complicating the view with conditional logic.

Using Generic ViewModel Protocols with ObservableObject in SwiftUI MVVM

Поделиться в:

Доступные форматы для скачивания:

Скачать видео mp4

  • Информация по загрузке:

Скачать аудио mp3

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

Как сжимаются изображения? [46 МБ ↘↘ 4,07 МБ] JPEG в деталях

Как сжимаются изображения? [46 МБ ↘↘ 4,07 МБ] JPEG в деталях

Синьор 1С: 10 привычек, без которых ты не вырастешь

Синьор 1С: 10 привычек, без которых ты не вырастешь

HP Gaming laptop

HP Gaming laptop

Декораторы Python — наглядное объяснение

Декораторы Python — наглядное объяснение

Microsoft продолжает попытки перевести всех на Linux.

Microsoft продолжает попытки перевести всех на Linux.

Typst: Современная замена Word и LaTeX, которую ждали 40 лет

Typst: Современная замена Word и LaTeX, которую ждали 40 лет

Но что такое нейронная сеть? | Глава 1. Глубокое обучение

Но что такое нейронная сеть? | Глава 1. Глубокое обучение

Код работает в 100 раз медленнее из-за ложного разделения ресурсов.

Код работает в 100 раз медленнее из-за ложного разделения ресурсов.

Учебник по React для начинающих

Учебник по React для начинающих

Пайтон для начинающих - Изучите Пайтон за 1 час

Пайтон для начинающих - Изучите Пайтон за 1 час

FFmpeg: бесплатный видеоконвертер из командной строки

FFmpeg: бесплатный видеоконвертер из командной строки

Где начало СХЕМЫ? Понимаем, читаем, изучаем схемы. Понятное объяснение!

Где начало СХЕМЫ? Понимаем, читаем, изучаем схемы. Понятное объяснение!

Срочные переговоры с Путиным / Вывод части войск

Срочные переговоры с Путиным / Вывод части войск

Microsoft begs for mercy

Microsoft begs for mercy

Neovim 0.12: новый менеджер плагинов и минимальный IDE-конфиг

Neovim 0.12: новый менеджер плагинов и минимальный IDE-конфиг

ИИ - ЭТО ИЛЛЮЗИЯ ИНТЕЛЛЕКТА. Но что он такое и почему совершил революцию?

ИИ - ЭТО ИЛЛЮЗИЯ ИНТЕЛЛЕКТА. Но что он такое и почему совершил революцию?

Почему Трамп в последний момент отменил удар по Ирану

Почему Трамп в последний момент отменил удар по Ирану

Если у тебя спросили «Как твои дела?» — НЕ ГОВОРИ! Ты теряешь свою силу | Еврейская мудрость

Если у тебя спросили «Как твои дела?» — НЕ ГОВОРИ! Ты теряешь свою силу | Еврейская мудрость

Working with C and C++ in Apple environment - Pragma Conference 2023

Working with C and C++ in Apple environment - Pragma Conference 2023

Вся IT-база в ОДНОМ видео: Память, Процессор, Код

Вся IT-база в ОДНОМ видео: Память, Процессор, Код

© 2025 dtub. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: infodtube@gmail.com