Check out a free preview of the full iOS App Development with Swift course

The "Environment Objects" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates using View structure objects called environment objects to avoid passing data manually as properties. Environment objects can be injected at any level.

Preview
Close

Transcript from the "Environment Objects" Lesson

[00:00:00]
>> We were saying that we need a way to access this from the view, from the user interface. And we need a single source of truth with I don't wanna pass this array with properties. So we have a better way, something known as embodiment option. So embodiment option is a way that we have to inject in the view hierarchy objects that anyone in the hierarchy can use.

[00:00:32]
And they work like Singleton's so actually. If these managers are actually good candidates for that, make sense? So we inject in the anywhere in the hierarchy these managers. So anyone can access the menu manager or the cart manager. That's kinda idea. Does it make sense? So how to do that?

[00:00:58]
First we need to pick where in the hierarchy we went in check this, anywhere. But if it's something global, most of the time, we can go as app as possible directly to the app itself. Okay, to the app itself we can inject directly here to the content view for example we can inject.

[00:01:25]
This embodiment option and how to do that through basically, our old friend modifier. Embodiment option and it says that the object that we can inject has to comply with observable option. So that's a protocol, okay? So if we wanna inject an object so what's the idea here? The idea is that I can create here a variable for my Menu Manager.

[00:02:01]
I can create an instance of it and another one for my cart manager. It's kinda empty at this point like so. So if I wanna check those by weights one by one. So I can add Menu Manager and I can add cart manager has two different modifiers. But they must implement they must conform to the protocol.

[00:02:30]
Observable option it's not gonna work okay does it make sense. So actually when we wanna inject this Singleton's these global objects, those objects must implement a protocol. So now if I'm gonna Menu Manager how to implement the protocol, just column and the name of the protocol. So observable options.

[00:02:57]
So this protocol in particular is not forcing me to do anything. Just with that slide it's good okay. So I'm adding Colin observable option on Menu Manager and cart manager like that. And what we need to do is the variable that we wanna use to subscribe bindings to something new at published.

[00:03:29]
So if we want views to have some bindings to that variable then we need to add published is actually a simple way for know when that menu is changing. It's another annotation similar to state. So we need an observable object with typically one or more variables that are published.

[00:03:57]
Okay, now we'll see how to use this. Any question? It's getting more abstract at this point I know but at the end we will connect all the dots. Of course we don't want this we wanna go to the network and download this product, okay. For that, I already have an API.

[00:04:22]
It's available. So if you go back here to working with data, we have already used data manager using environment object. While we didn't create the car we can actually do that, we can do that in a minute. But before doing that, I'm gonna show you the JSON file.

[00:04:44]
This is the JSON. That as you can see, has an array of categories with products inside. That is matching my structure my data model. Okay, so what I want now is I wanna download that JSON parse the JSON. I know it sounds scary, but you will see, it's actually pretty simple and straightforward.

[00:05:09]
And we wanna feel that array that collection and then render that on the screen.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now