Defining Custom Environment Keys in SwiftUI

Gabriel Theodoropoulos
5 min readOct 29, 2021
Photo by Jason D on Unsplash

When implementing apps using SwiftUI, we often need to access a view’s environment and read values from it. It’s easy to give environment a picture; think of it as a memory pool containing global values, shared and accessible all the way down to the view hierarchy.

SwiftUI framework sets several values to the environment by default. We can get them by declaring properties marked with the…

--

--

Gabriel Theodoropoulos

An iOS & macOS app maker writing code in Swift. Author of countless programming tutorials. Content creator. https://serialcoder.dev