With iOS17 comes an easier way to manage data in Swift UI apps. In this video let's discover SwiftUI's
groundbreaking ESB architecture. That is the @Environmentm, @State, @Bindable architecture.
Master @Environment, @State and @Bindable for efficient data management. Join me as we explore global
data access, local manipulation and seamless data binding. Go ahead and check out my Summer Sale the link
is down in the description. So today we are going to talk about data handling in SwiftUI5, that is
IOS 17 and I have created already a data handling in SwiftUI epic video on that but that was SwiftUI4.
Now things have changed quite dramatically. So we are going to address those. I am using Xcode 15 beta 2
so make sure that you are on at least on a beta 2. So what I have here is a simple
app with a content view. So we are going to take a look the ESB architecture. That is @Environment,
@State, @Bindable architecture. So I have my content view but we are going to also navigate. So first
thing that I will do right over here is create a new SwiftUI view, that is the detail view.
Detail view just like that. Let's create it and that's it. I'm going to just say hello detail view
for our text right over there, great, okay. Next up we want to talk about this ESB architecture.
What is the @Environment, the @State and the @Binding because previously we had state object, observable
object, observed published, all of that stuff. Now it's really, really straightforward
and simple. Calling or SwiftUI enthusiasts. It's time to ignite your development journey with my
exclusive Summer Sale. Enjoy an incredible 70% discount on all my top notch SwiftUI products.
Don't miss this golden opportunity to level up your skills at an unbeatable price. The discounted
link is in the description. Go there now and unlock the full potential of your SwiftUI projects with
my amazing Summer Sale. So we have contentvView and you know MVVM, you might have a view model in
this content view. So I will just call this not view model but view state. So I'm going to add it
onto this file, into this particular file, I'm going to make it five private. So it can be accessible
only on this file, only on the content view. So this will be a file private class, view state
and I'm going to have only one property right over here var is detail view presented and the default
value will be false, okay. So if you know SwiftUI4 previously how we made this could be an observable
object and then we have our published variable, is detail you presented but we don't have to do that
at all in a SwiftUI5. All we have to do is just add the @Observable property wrapper right over here.
@Observable, it's not the property wrapper it's actually a macro, So @Observable flie private
class view state. So now we can access this file private on the content view and we access it not
through a state object just simply through the new polished state. So @State private yeah
it's going to be private so you know we cannot initialize the content view with it, var view
State, there we go and this is our view State, there we go. This is our view State. So now we can
actually use this. So what I'm going to do is just create a simple button. So let's just have our
button right over here with a title of detail view and the action, well we just go view state dot is
detail view presented dot toggle and in just a few lines of code. It's so fantastic. It's as
easy as that. So let's build and run and let's see how that looks like but before we do that let's
just add in here our sheet with the is presented and content dollar view state dot is detail view
presented. That's what we are binding it too and the content is our detailed view, okay. Let's hit
Command + R, don't ask me again and replace Command + R to build and run and tapping on detail view
we'll just present that sheet. It's really, really nice with just a few lines of code as I told you.
Okay! So that is our observable and this is our state. So what we want to do basically is just not
have only these view states, these MVVM style view models. I want this to be available throughout our
application. So I want this to be on an app level not just the view level observable. So for that
well we are going to use a person. So let's just create our model right over here. So let's create
a new file Swift file, next person and that will be just a simple struct, person. The simple struct that
we could just have here var name string and it's equals and empty or we could just make it even
shorter just like that, okay. So now we could just use this person and have it available throughout
our application and for that we would just create another observable which will be put into the
environment. So let me just show you how easy it is to do that. We're going to create another Swift file
Let's name this app state or you can just create any other state like profile state settings
or service state whatever. I'm going to name it app state and I'm going to import SwiftUI and then
create @Observable class, the small 'C' there, class AppState and we are going to create our person
equals person that's right and I'm also going to create some logic right over here.
Let's just add a function of fetch person there we go. And that's going to basically have person
equals our person initialized with the name of Ada and basically that's it. Now that we have our
Upstate ready we need to have it available throughout our application and you can just
add this basically on any kind of root level that you choose but currently I'm just going to go all
the way to the content view for the root of our application and again we are going to use State,
it's as easy as that. So let's have a private State variable and equals AppState and what we want to
do is set it up as an environment. So if you type out environment you will see that there's a new
one with this Observable Object. So we are going to add that AppState right over there, okay.
So now that we have the environment how can we access it? Well it's as simple as grabbing the environment
on our view. So you just go @Environment, open parenthesis and then you want to set up the
observables protocol. So basically the name of our observable so appstate.self var AppState, come on
State and basically that's it. We have our, yeah maybe we could just make this private.
There we go. So now we can just access our AppState and our profile. So let's just have a little
bit of a font right over here. Let's have a text for our AppState.person.name. So we are going
to display our name and then we are going to have a button right over here with the title of fetch
and then the action would be our AppState dot fetchperson. So let's hit Command + R and let's see
how that is going to look like, there we go. We just go fetch and Ada has been added onto that text,
which is really nice. By the way the detail view is still working as expected, okay. So that's the
easiest way to just add in an environment. But what if I want to change this name. I want to bind this
to a text field? Well you could create a bindable. Now you cannot actually add. So let me just add in
here a text field just for, just showing you to try this out. So we're just going to have a name and
the text would be, you see it's accepting a binding of time stream. So you could just type out @AppState
but as you can see it's not auto completing dot person dot name, but you know it's not going
to access this. So for that we need to create a binding. So we are going to create a new file.
SwiftUI view, next and that will be bindable text field, okay. So now we just create and right
over here I'm going to just simply copy out our text field. So this is actually what we want. So I'm
going to cut it out actually. So putting it right over here. We are going to need this app state.
So how do we get it? Well with the bindable. So @Bindable var AppState of type AppState.
And there you have it. Now we have a bindable. So of course we can just remove the preview. So right
over here on the content view now we can just use our bindable text field with our AppState. So just
pass along our AppState and it should just work as expected. So there we go, if we just type in here
maybe whatever Bob, there we go. It is a binded as expected. Now this is kind of an extra step.
I don't really like making custom bindable views in our SwiftUI app, therefore there's an easier way.
So let me just comment that out and let's just type out again our text field and we have
our title key of a name and then here we need actually a bindable. So let's just have a bindable
with a wrap value. Now the wrap value is our AppState and then we can just access the person.name.
And if we hit Command + R you will see that it's everything as it should be. So Ada right
over there or Bob right ohere there. It's really, really fantastic. Now if you did like this video
about data handling, make sure to check out the new iOS17 SwiftData CRUD. The video is right over here.