Book Image

An iOS Developer's Guide to SwiftUI

By : Michele Fadda
Book Image

An iOS Developer's Guide to SwiftUI

By: Michele Fadda

Overview of this book

– SwiftUI transforms Apple Platform app development with intuitive Swift code for seamless UI design. – Explore SwiftUI's declarative programming: define what the app should look like and do, while the OS handles the heavy lifting. – Hands-on approach covers SwiftUI fundamentals and often-omitted parts in introductory guides. – Progress from creating views and modifiers to intricate, responsive UIs and advanced techniques for complex apps. – Focus on new features in asynchronous programming and architecture patterns for efficient, modern app design. – Learn UIKit and SwiftUI integration, plus how to run tests for SwiftUI applications. – Gain confidence to harness SwiftUI's full potential for building professional-grade apps across Apple devices.
Table of Contents (25 chapters)
Free Chapter
1
Part 1: Simple Views
5
Part 2: Scrollable Views
8
Part 3: SwiftUI Navigation
11
Part 4: Graphics and Animation
14
Part 5: App Architecture
17
Part 6: Beyond Basics

MainActor

MainActor is a special actor that is tied to the system level and is always present in a modern Apple app, that is based on a version of the operating system that is capable of supporting structured concurrency.

MainActor is tied to the main queue. Any update to the UI in Apple systems needs to be performed within the main queue.

Any code marked with @MainActor will execute on the MainActor, which is tied to the main queue, and this is particularly useful to ensure that UI update-related code is not executed on threads different from the main one.

For instance, the code shown in the following fragment would be executed in MainActor:

@MainActor
private func showAlert() {
        isAlertShown = true
}

And this would be needed if we wanted to be able to, for example, invoke the showAlert() function from a within background task, but with the need to affect the UI.

@MainActor can be used with closures, classes, structs...