© Gio Lodi 2021
G. LodiTest-Driven Development in Swifthttps://doi.org/10.1007/978-1-4842-7002-8_11

11. Injecting Dependencies with @EnvironmentObject

Gio Lodi1  
(1)
Mount Martha, VIC, Australia
 

How do you share the same instance of an object between SwiftUI views without injecting it through all their common ancestors in the view hierarchy?

By using @EnvironmentObject to access it from the environment, the context in which all the views live.

In this chapter, you’ll learn how to share business logic components between views with @EnvironmentObject in a testable way.

Our app is getting closer to being fully functional. To get there, we need to build the UI and business logic to let customers select dishes and make their orders.

What’s the Earliest Testable ...

Get Test-Driven Development in Swift: Compile Better Code with XCTest and TDD now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.