Sitemap
Expert App Devs

Expert App Devs

We are a 16 year old bespoke iOS & Android app development company from India. Find the latest updates & insights on mobile apps. https://www.expertappdevs.com

Sorting Out Your App Architecture With SwiftUI Design Patterns

5 min readDec 6, 2023

--

Sorting Out Your App Architecture With SwiftUI Design Patterns

When building an app with SwiftUI, it’s important to have a solid understanding of design patterns and how they can be used to create maintainable and scalable code. In this blog post, we’ll explore some of the most commonly used design patterns in SwiftUI app development.

Model-View-ViewModel (MVVM):

MVVM is a popular design pattern in iOS development, and it works well with SwiftUI. The basic idea of MVVM is to separate your app’s business logic from its user interface. The Model represents the data and business logic, the View displays the user interface, and the ViewModel sits between the two, handling the communication between them. SwiftUI makes it easy to implement MVVM since it provides native support for binding between views and their underlying data.

let’s define the basic components of the MVVM architecture:

  • Model: Represents the data and business logic of the app.
  • View: Displays the UI elements and handles user interactions.
  • ViewModel: Mediates between the Model and View, providing data to the View and handling user input.

Here’s an example of a simple SwiftUI app that displays a list of products and allows the user to filter them by category using the MVVM architecture:

  1. Create a new SwiftUI project in Xcode.
  2. Create a new Swift file for the Model. In this example, we’ll call it Product. swift.
Press enter or click to view image in full size
Create a new SwiftUI project in Xcode.

3. Create a new Swift file for the ViewModel. In this example, we’ll call it ProductListViewModel.swift.

Press enter or click to view image in full size
ProductListViewModel.swift
ProductListViewModel.swift
ProductListViewModel.swift

4. Create a new Swift file for the View. In this example, we’ll call it ProductListView.swift.

Observable Object:

An Observable Object is a class that can be used to store and manage the state of your app. It allows you to declare a property as @Published, meaning any changes to that property will automatically update any views bound to it. This powerful feature of SwiftUI makes it easy to keep your app’s state in sync with its user interface.

Let’s create a demo with an Observable Object

First, create an ObservableObject class. This is the class whose properties you want to observe for changes. In this example, we’ll create a User class with a name property:

Press enter or click to view image in full size
we’ll create a User class with a name property

Note the @Published keyword before the name property. This tells SwiftUI to automatically update any views that depend on the name property when it changes.

Get Expert App Devs’s stories in your inbox

Join Medium for free to get updates from this writer.

Next, create a SwiftUI view that uses the User object. In this example, we’ll create a view that displays the user’s name and allows the user to edit it:

Press enter or click to view image in full size
SwiftUI view

Note the @ObservedObject keyword before the user property. This tells SwiftUI to monitor the User object for changes and automatically update the view when it changes.

Finally, create an instance of the User object and pass it to the view:

Press enter or click to view image in full size
create an instance of the User object and pass it to the view

Dependency Injection:

Dependency Injection is a design pattern that allows you to separate the creation of objects from their use. This can help make your code more modular and easier to test. In SwiftUI, you can use EnvironmentObject to inject dependencies into your views. EnvironmentObject is a way to pass an object down the view hierarchy so that it can be used by any view that needs it.

Let’s create a protocol that defines the dependency we want to inject:

Press enter or click to view image in full size
Let’s create a protocol that defines the dependency we want to inject:

Next, let’s create a class that implements this protocol:

Press enter or click to view image in full size

Now, let’s create our main view, ContentView, that displays the data fetched from our NetworkService:

Press enter or click to view image in full size
the data fetched from our NetworkService

To inject the NetworkService dependency, we’ll create a Container class that holds an instance of the service and provides it to the views that need it:

Press enter or click to view image in full size
create a Container class

Finally, we’ll use the @EnvironmentObject property wrapper to inject the NetworkService into our ContentView:

Press enter or click to view image in full size
EnvironmentObject property wrapper

And that’s it! Now our ContentView is using dependency injection to fetch data from the NetworkService. To use this in your app, make sure to set up your Container instance as an environment object at the top level of your view hierarchy:

Press enter or click to view image in full size
ContentView

Using these design patterns, you can create SwiftUI apps that are easy to maintain, test, and scale. With its support for binding, observable objects, and dependency injection, SwiftUI provides powerful tools for building modern, reactive apps.

If you have any queries regarding the iOS App, Hire iOS App Developers India from us.

--

--

Expert App Devs
Expert App Devs