hi Alex here from rebeloper.com and welcome back to ASD App Store Domination
and today we are going to learn how you can best store constants or well the way
I like to store constants the best way. So without further ado let's get to work!
In your app you might have constants like how many levels there are
or what is the base color of your app. For that you need to store them in some
sort of way that is accessible through all over the classes and for that I
usually create a surface a class. It will be a static... well we are going to store
these constants as static constants. And let us just
open up Xcode and let me just close this and create a new project. It will be a
Single View application and let's name this... Okay let's click on create and
let's drop it onto the desktop. No need to create a git repository for this
because this is just a simple example. Let's go into ViewController and let's
adhere our view.backgroundColor as let's say UI color and I will
construct it not to it the simple .purple or .red but with this red, green,
blue and alpha. If we and if you want to create an extension go ahead and take a
look at the previous video where our create a much more simple extension of
our UIColor with RGB values only. I just type in here...
and alpha value 1.0 of course! Now this will give me a background that is
bright blue. Now if I want to use this blue color in...
Let's run it on iphoneX simulator.
If I want to use this color on multiple view controllers of multiple
views then I would need to type this again and again in and this is prone to
typos. What I usually do is create constants for this type of things.
I will just simply create a New File here. I will just call it service and now
I will import UIKit because we are going to use a UIColor. Let's create
a new class here and name it Service and it will not be of any specific
superclass. And inside here we are going to create static let constants... Okay!
And I will just copy out what I have added here. I will just cut it out
because I want to use the new syntax. There you have it! Here we have let me
just take that in so you may see. Here we have our base color as a static constant.
Let's go back to view controller and let's build so we may get the completion.
Command + B, maybe just stop this and hit command + B again. h it is indexing we
might want to close this. Let's stop this task because sometimes it takes a really
really long time to afford it to index. This is most probably and
Xcode9 issue. I'm just force quit this. App development in action! Let's
open up Xcode again. Okay! It is indexing and now it should be
ready. Yes, it has indexed. For some reason it just stocks there. Okay, and now
I will type in Service and with the (.) syntax we have our a base color. There
you have pretty simple. Okay! Let me just close this. Let's build and run on our
iPhoneX simulator to test it out if this really actually works. Yay, it does!
And of course you can add here static let numbers or strings or anything let's
say an ID so anything that you like. And of course you can create functions too.
Let me just type in here a new functions, so static ... this is just a dummy
function and I will just print out here... pretty dummy example here but I will
just type in here Service. And let's just build here so we may get
the completion from our Service for our function and here it is new function!
If we build and run again now we should see in the log that it has been printed
out started app. Pretty good stuff! Now you know how you can use
something called as static functions and static constants inside over let's say a
service class. Okay! Hope you enjoyed this video and as usual I will see you
in the next one! Good stuff! Now if you want to see more of this video and want
to boost your app business go ahead and take a look at my 30 Days Challenge App Store Domination!
Go ahead and click the link down below and I will see you
inside ASD - App Store Domination! Let get to work!