The @Environment && @EnvironmentProperty SwiftUI Property Wrappers

Update views with ‘em

Steven Curtis
3 min readJun 14, 2023
Photo by Grant Ritchie on Unsplash

Although I’ve labeled this article as beginner, I’ve noticed that my previous article about property wrappers just didn’t make it seem so. So I think it really asks for a new article about the @Environment property wrapper.

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

Terminology

Property Wrapper: A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property

@Environment: a property wrapper that allows your views to access and react to system-wide settings and conditions

@EnvironmentObject: a property wrapper that allows your views to access and react to custom-defined settings and conditions

The basic examples

@Environment

The classic example of a system-wide value is colorScheme, so the view can know if we are in dark or light model. A simple example can be the following

struct ContentView: View {
@Environment(\.colorScheme) var…

--

--