Hi Alex here from rebeloper.com and welcome to another great ASD episode
AppStore Domination and today we are going to talk about extensions, so
let's get to work!
Now what are extensions? Well if you recall programming there are a lot of
classes provided by Apple or even if you create them and you can add extensions
to them. So today we are going to create one of those extensions and most
specifically we are going to create a UI color extension. Okay! Let's get to work
by opening up Xcode and let's create a New Project here. Create a new project
Xcode file. It will be a Single View Application and I will call this... Okay!
And let's click on Next. I will drop it onto the desktop and I will just uncheck
create git repository because this is a simple project we are not going to
create any backups for it or even upload it to GitHub. Here we are inside our
project let's select our ViewController and inside the viewDidLoad let's add our
view.backgroundColor = let's say
.purple and now let's select our iPhoneX simulator and build and run. Okay!
Let me just resize this so you may see our simulator here. And here we are!
We have a purple background and if you know some Swift coding you know that this a
.syntax comes from UIColor .purple. Okay! This is how you can add a
default color that is specified by Apple. Once again if I just type in here like
so we can create a UIColor with the hue and saturation,
named with a red green and blue and alpha value, with a white and alpha value
and so on. Okay! Most importantly we are going to
focus now on this red, green and blue and with alpha value. Now these are CGFloats.
Let me just open up here Photoshop, so you may see what are these red, green and
blue and alpha value mean. Okay! Net's create a new project and it
doesn't really matter let's just create this one. And now I will just selec, well
let's see this bluish color. And I will just open up our color picker and if you
can see here we have a red value of 0 a green value of 114 and blue value of
188. And now these values come from 0 to 255 and this is how the colors are
created here. If you just drag and drop here and move this around you see that
all this red a green and blue values change. Let's go back here and
copy our numbers out and 0 it means that 0 / 255. Next up we have let me just take
this up here 114 / 255. And the blue one is one eight 188 / 255. Okay and
now for the alpha value that is our alpha channel how transparent this
should be. So for now I will just set this to 1.0 because I want it
to be not at all transparent. Let's build and run
again and see that now we will have this bluish black background and an alpha value.
And as you can see the bluish color is a little bit darker. Okay! Let's set this
back to 1.0 and you will most probably always want to have this alpha
value as 1. And what I really don't like about the disk constructor is that I
always have to type in... let me just build and run again. I always have to type
in this / 255 so we are going to create an extension here. What does an
extension look like? Well you just simply type out... and now you will need to
provide some code. Well let's say helper function to make this UIColor
look like and wouldn't it be easier if I could just type in here... and
for the green just a simple G and it was a 114 I believe and for the blue 188
and without any alpha values. Wouldn't it be great so just add these three values
and you are off. Well with extensions you can do that and of course because we
haven't created our extension yet we have an error here. Let's create it now.
Okay! First of all we are going to create a convenience public
function that will house the alpha value too... And we are going to add here our
rad it will be a CGFloat, our Green CGFloat
and blue CGFloat and of course our alpha value here as a CGFloat again.
And inside our function we are going to type in self init and we are going
to use the other constructor we have just seen back here and let me just
write this out again... We can simply copy this out. Here we have it red,
green and blue. I'll just paste this in here and for the red I will just type
in here r, g, b and a. Okay and let's just build and run and what is the
problem we don't need this function. Okay! Let's build here and of course
we don't have our UIColor here available so I just replaced with this with red for
now so let's build again and we should have a build succeeded. Now that we have
this convenience initializer we can go ahead and let's say type in UIColor and
now let us just find this r, g, b, a and we can now add our values. But I don't want
to add 0 / 255 so I just simply add here 255 divided 255 and 255. And now I
just simply need to add those values from Photoshop. Let me just open that up
again so what I want to do here is 0 ,114 and 188 and for the add value of
course 1. And if we build and run again you will see that we have that
simple blueish background. But once again as I told you I want to get
rid of this alpha value if I want wanted to. What I will do here is well maybe
let's make another convenience in it or maybe just use this =1. Okay!
And maybe make it an optional I believe that is the way to go. Let's start all over
again and let us just type in... Let's build for now. Okay! I don't need
that optional, so UIColor and if I just go back here you will see that I already
have two convenience methods and I will just simply choose a this one.
So 0, 114 and 188 and that is all you need to do.
Let's build and run again with Command +B and you should see that beautiful blue color.
And there you have it you now you know how you can create extensions. Of course
you can create extensions for UIViews and everything in between or everything
you want to add a new convenience public initializer. Okay! Hope you enjoyed this
video if you did go ahead and play around with extension 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's get to work!