Hi, Alex here from rebeloper.com and welcome back to the channel. In this video
tutorial I will show you how you can set up configuration settings in your Xcode
project. So without further ado let's jump right into it.
Okay! Let me just show you what we are going to build out today.
And it will be real easy to do so let's
say you have some constants in your app like some IDs or anything like that and
you want to store them not inside your swift files but in need p-list file.
So here I have a dictionary called my dict it has a number with the key
value, a key called version and value of 5 and inside this root folder I also
have so called naked key pair with the key of language and the type of string
and the value of swift. So I would like to access all of these throughout my
code and I do access it inside my root view controller here and it's pretty
simple its config values get dot language and that would be of course
swift and with the same old dot syntax I just grabbed my version here with my dict
dot version. Of course that you will learn how to set these config values get
up in this video. So let's just build and run this and see what do we get in the
console, okay. There it is language is Swift 5.0, okay. Let's just stop this and
start off with a brand new project. There we go and I will use my single view
application a template go ahead and click the link down below for details on
how you can do that basically it removes the main dot storyboard and we do all of
our coding in code while coding in code yes without storyboards, okay.
So I will name this let see config swift. Okay! That's right. It will have swift
language and embedded in navigation controller. It doesn't really matter I'm
just printing out something in the log right now. So let me create a new folder
here real quick with the code and I will create a git repository on my Mac and I
will upload it to my github repositories so you can take a look at it later on.
Okay, let's create it here and let me just resize this so you can see it
better. There we go. Now you will see in the description a link with where you
can join rebeloper.com and you can get all of my resources and in resources you
will find a config dot swift file. I'm just going to drag and drop this here
because it's much simpler and I will just explain what the code looks like
and yeah this will remain all the same throughout your code base so this is our
config value struct and it has this static function called get it returns a
config and that is a decodable. Now I won't go into details why this needs to
be a decodable, but just make sure that under full resource you have this config
string and that is exactly the name of your plist file. So let's go ahead and
create it. Hit command N and new file template chooser will pop up. I will
choose let's just scroll all the way down till we see our plist file, where is
it, I missed that oh here it is property
list. Let's click on next there. So let's name this config but you can name it
whatever you want as long as you just set that here. So config and config, okay.
So let's start off by adding our language, okay. So what you want to do is
click on this plus button and call this language. There we go and it will be a
string. Now you have a lot of options array, dictionary, boolean, data, date,
numbers, string it you can choose any of these for now I just choose string
because swift is, string I just want to have this, okay. Now let's go back into
config and it says here under config struct that add keys here. So let's add
them here. Let's see let language and make sure that this is the same as the
key that you sat in the plist file and that will be of type string, yep.
Let's hit command B so everything should compile and yeah let's go back to our
root view controller and here let's just print out our language. So print language
is and now we just simply get that with config values dot get so we
are getting all of our plist here and then with the dot notation we get our
language and yeah it's just build and run and see oh not on my phone let's
select a simulator here and we will see that we will get this language is swift
in the console. Just wait for that and there we go language is swift. Now if you
recall in the example we don't want to add everything in the route because
maybe we will have a few tens of keys here and yeah we want to have
dictionaries. So let's add in here let's say a my dict, dictionary and select
dictionary here and open it and click on this plus button again and let's add in
here version, okay. And this will be a number and version will be five okay and
that is basically what you want to do in your p list file. Now let's go back to
our config and what you might want to do right off the bat is add in here like
let my dict and that will be a dictionary but that
will not work basically. So let me just uncomment this you need to create a
struct called my dict and that confirm to the decodable. So let's see here
struct my dict and that conforms to the decodable protocole and here we just
add in some new values like version and that will be now if you see we have a
number here. Now when it decodes it you can add anything here like integers,
doubles, CGFloats. So I will add in here a CGFloat just for example of course
you basically cannot add in here 5.1 but yeah that is one row bad but you could
let's say come around with a nice hack for that, okay. Here we have our version.
Now we need to declare it in the config decodable in the struct and that is
of course of type not dictionary but my a dict.
And yeah that is all you have to do. Let me just go to our root view controller and
let's just add in here config values dot get and now I type in here let's hit
command B so we get auto completion from xcode my dick and a version.
There we go. Pretty simple. Hit command R and now we will see that we have language is
Swift 5.0. Now that is because we have unwrapped it inside a CGFloat and if we
would let's say add in here let say int then we would have just swift 5 but most
probably you will use use CDFloats or maybe doubles. So yeah 5 let me just make
that back to CGFloat. Now that is it for this really easy configuration of
constant values in your Xcode project of course all of this in Swift 5. Now how
amazing is that. If you like this video go ahead and give
it a thumbs up, subscribe to the channel and hit that notification bell because
even if you subscribe you may not get the latest videos on your feet so hit
the catification back I dare you and as usual I will see you in the next one.