Writing your first SwiftUI app is one of the easiest things you can do. But it's also one of
the hardest things to get right. So in this video I have compiled four years of working with SwiftUI
into the 10 commandments of SwiftUI. I have condensed my experience into this actionable
insights so you don't have to go through the trial and error I needed to go through. If you're brand
new to Swift UI development or even if you've been doing this for years you will surely benefit from
these evergreen principles. So let's get into the 10 commandments of SwiftUI. The first commandment
is you shall have one source of truth and this is so true about SwiftUI. You want to have only
one source of truth. If you have multiple sources of truth you will have like the spaghetti code.
You will not know where the data lies. You will have views not updating properly because you
have multiple sources of truth. Basically wherever you instantiate an observable object with a state
object you know that that is your source of truth. Now number 2: Data shall Drive the UI.
This is the second commandment of SwiftUI that data is driving everything in SwiftUI including
the UI. So whenever you are building out SwiftUI apps you want to think of it as a data management
perspective. So data goes through stages of course through time and the UI will automatically
update itself according to those data changes. Yeah, it's really, really important to grant space it's
the or quite opposite of what we have in UIKit but this is really, really super fun if you adhere
to the first principle that you shall have only one source of truth for one a particular object
then this second one data shell drive that UI is quite easy to follow up on. Oh, hey, sorry to
interrupt before we get into the next commandment I want to take a step back and invite you into
SwiftUI Nation. My private Community. Swift UI Nation and the name implies is a community for
SwiftUI developers. Members have access to me 24/7 through the SwiftUI Nation Discord server and
get weekly one hour group coaching but you can ask me anything. On top of that you'll get access
to the members only SwiftUI helper framework that helped me build SwiftUI apps for my clients
10 times faster and easier. Boost your SwiftUI knowledge, join SwiftUI Nation today. Link is
down in the description. Commandment number 3: Is you shall construct your views as a recipe.
Now in UIKit we are adding in UIViews, UIButtons, UITextfield, UICollectionViews and all of those are
kind of objects that you kind of see that they are already on the screen. Now in SwiftUI it's quite
the opposite. You are not adding a button, your not adding a scroll view, you are not adding a text. You are
simply creating a recipe for SwiftUI to kind of know where to put your views, what to do
with them, according to your data changes. It is only a recipe. That's why it's called the struct
and we are not using classes, okay. Commandment number 4: Honor the four types of views.
Now what are the four types of views? Actually if you know the first two then you will know, kind
of know the last two. The first one is the pull in view. Now pulling views are those views that
are going to try to pull every other view around themselves to themselves and that is the pulling view.
Now the push out view, that's the second type of view is quite the opposite. They are trying to
stretch themselves as much as possible to take up as much possible as space as they can. Now the
third one that's kind of an outlier it's the image. Not the system name image, it's just a simple image.
And that is one that you add in your assets and you just call image and you just add in the name
of your image. Now for that you want to make it resizable and you also want to add a frame to it.
Now the frames are the devil, I'm going to talk about that in a upcoming commandment but let's go
on to the type of view that we just have kind of mix and match with and that is the divider. Now if
you put the divider into a horizontal stack it will be a pull in view in this way and push out
to you on the other way or axis I mean and if you put them onto a vertical stack then it will
be the other way around. So it's kind of a pull-in view and a push out to you at the same time but
according to the container that you are adding to into it. It will behave differently, okay.
Commandment number 5. You shall avoid frame. Now why is that, because we talked about the image
previously and you needed to add a frame to it because you add it as a resizable so yeah you
want to add a frame to it but what about other views. Now you should avoid frame as the plague
in a SwiftUI application. You want your recipe to be without a frame. So SwiftUI can lay out
your views according like in a manner like really, really stylishly I would say. Because if you add
a frame to it like a constant width and height or either of those then you will have some surprises.
So avoid the frame as much as possible. Commanded number 6. Remember to encapsulate your
view models. Yes! MVVM works really, really fine with SwiftUI but you have to remember what the view
model actually means. It's a view model for your view. So what you want to do is not let it loose,
so to speak, you want this to be constrained for your view only. What this means in coding terms is that
you actually want it to be a private extension on your file that contains your view. I have previous
videos on that so go ahead and check them out. Number 7. You shall not abuse the environment object.
And it kind of comes from the previous one like of course you want to create your environment
objects so you have different published variables available on multiple views so you don't have to
inject them into your views over and over again down let's say navigation chain so you are
creating an environment object, but you have to be really careful because now that published variable
will be available in all of your views, all the time. So yeah you gotta be really, really
specific on what views you are going to access your environment object. Commandment number 8.
You shall refactor large views. Now this is a no-brainer and also Apple recommends this.
If your view is let's say longer than around 100 lines then most probably you have a good chance to
refactor your view. Make sure that you subtract and refactor of course your sub views or some
parts of your view like in terms of functionality. Make sure that you do not have large views.
It's a pain to read and you will thank me later after two months. Commandment number 9. You shall avoid
stringly typed code. Now SwiftUI uses some stringy correct code. So for example for the image named.
So you are going to type in the name of the image as a string or also for the system name images,
you know the SFSymbols. You are going to type out a string. Now that is really, really dangerous in my eye.
So what you want to do and just simply create an enum for all of those strings
just type it in once, make sure that you type it correctly and then you can just use it everywhere.
And finally commandment number 10. You shall never say that SwiftUI is not production ready. Now this
is kind of obvious. Everybody's talking about how production ready SwiftUI is, believe me it is, but
that's just my opinion. So what you want to do is just build apps with SwiftUI. Go ahead, take
it for a spin, try it out and if it is production ready for your type of app then it is and if it's
not then of course you can just go and deep down for UIKit. And of course SwiftUI goes
really, really well with the preview provider. So you can just work really fast but I highly
recommend that you take a look at hot reloading your simulator. The video is right over here.