Hi, Alex here from rebeloper.com and in this video I am going to show you some
code for Swifty Plist Manager. We are going to get save and fetch some values
from our plist files. So without further ado let's dive in.
First of all Swifty Plist Manager needs a default starting plist file. So if you haven't already
downloaded the master Swifty Plist Manager then go ahead and do so and
inside of it you will find a Data.plist file and an OtherData.plist file.
So we will just move all of them, both of them into our project. Okay!
Let's just click on finish...
And now we have our Data.plist file and our OtherData. We will leave the
OtherData.plist file as is, as an empty because this will be only for an example
and we will use our Data.plist file. For now this is an empty plist file.
So let's add a new value to it. So I will just click on this little plus button
and add our let's say newKey and for the value it will be a string and I will
say Hello SwiftyPlistManager, great!
And here we are. We have a Data.plist file with one key value pair, great. Now
let's go back to viewController and we should start Swifty Plist Manager.
But before we do that I will let you on a little secret and that is to be typo
safe. And we will do that with the help of some constants. And those constants
that I will use in this tutorial are found again in our downloaded folder.
Inside the viewController file. So go ahead and double click on it to open it
and just scroll down and here you can see that we have a lot of constants and
one variable, okay. Let's just copy these out and add them to our
viewDidLoad function. Xcode will complain and there will be a lot of warnings here
they are but once we will use all of these constants these
warnings will go away, okay. Now the first constant is DataPlistName and
that is the data and as you can see we have an OtherDataPlistName and OtherData, great.
And what we should be aware of at the moment is that we have a newKey
constant with the newKey value, okay. Now back to Swifty Plist
Manager. In order for Swifty Plist Manager to work we need to start and we
need to start it only once but we need to start with a plist file. At least a
plist file. So let's do that. SwiftyPlistManager.shared and
find start. And as you can see we have a plist names an array of strings and
logging, it should be a bool value true or false and logging if it is true we
will see a lot of logs in a console if it is false we will see nothing. And of
course if you haven't already done so we need to import Swifty Plist Manager.
Okay let's hit return. And now I will just enter of course this will be an
array but I will just enter the data, okay. And that is the data plist name
and see how constants come in handy now. And for the logging it should be true of
course because we need to see it what happens behind the scenes, okay. Let's
just build and run and test it out on a simulator of course the simulator
doesn't matter what it says it will be a blank screen but we should have a
successful build and some logs in the console. And here we are starting Swifty
Plist Manager. Initialized Data.plist and initialization complete, great.
Good stuff here and now I will just add another plist name and that is other
data plist name. And let's just build and run that too so you may see that we
can get an array of plists. That is correct initialized OtherData.plist.
Okay fun stuff here, now we have started our Swifty Plist Manager
with plist files and we just delete this for now because we are
going to use only the DataPlistName. And now we want to get the value from
our Data.plist file and the value that is for the key newKey. So let's do that.
Type in SwiftyPlistManager.shared
and here we have a bunch of options but I will use for now get
value and for what is the key name and the key name is newKey of course and
from plist wit name and that is the DataPlistName you should, you
could also use the OtherPlistName if you have started it. But for now we will
use DataPlistName. And here we have a nice completion hit return and we will
have a result. And let me just go back there as you can see we have an optional
result and we will dive in later how to cause that out. We have a result here and
we have a Swifty Plist Manager error that is also an optional. So I will
type in error. And let's go to the code and first of all we need to check if
there was an error fetching our new value. So I'm sorry getting our new value
because in a moment you will see the differences between getting and fetching.
Okay so if error = nil so we don't have any errors then we can I just say
print it out. Print and let's say the value for
key and this is the key, newKey ...
... is and here we have our result. So let's just add our result here from the
plist and I will just type in DataPlistName, okay. Let's just see what
happens in instantly we have a warning because the result is an optional.
So let's just use the simple bank operator here for now and let's see what happens.
Okay build succeeded, great and here we have the value for key newKey is Hello
SPM from the plist data of course that is the name of the plist. I will just...
plist... and I will just add on plist here for the sake of being real nice here.
Okay, banking these optional results out is
pretty pretty dangerous. So one way to deal with this is let's say to add the
default value. So I will just type in here 'no value'. And if we wouldn't have
any value than this no value would be printed out. But since this is not the
case this will not happen. Another way a good way and the best way of course is
to use guard let statements. So let me just go up here and let's just cost it
out with a guard let statement. Guard let result = result.
Okay else we will return, okay. Now we have costed out our result
and we no longer need to worry about being it an optional. So we can delete
that and let's just build and run again and of course we will see nothing
different in the console the value for key newKey is Hello SPM from the
PlistData.plist, okay. But we are much... well 100% safe!
Another way to do that if you don't want to instantly return, if there is no
result is to use the if let statement. So I just delete this and I will if let
result = result then we will print out this into the
log. I would just copy this out and paste it here. The advantage over using
if let statement over the guard let is that when using the if let statement you can
still continue down the line of code. So I will just print out here ...
Okay that's just build and run and see what happens. If this print
statement would have been set after the guard let statement and the guard
let statement failed than this hello, I'm still printed out here wouldn't have
been printed out. But for the if let statement it would be. Even if the
result was nil, okay. Now that we have all that set up and we just delete this
and leave the if let statement because that's fine with me.
Let's just save another value for our key, okay. And that is pretty easy
swifty, oh inside the viewDidLoad statement please.
SwiftyPlistManager.shared and find the save value and for the key to the
plist with name and we also have a completion. So I will just return, hit
return on that. And now let's save it can be say anything but I will just save
this let just see, 'Hello New Value'. And just copied this out and paste it in. And for
the key that is the newKey because we want to change the current value and to
plist with name and that is the DataPlistName and we have here a
completion. And of course we need to check for errors. So here is our error
and just type it in if error = nil then we may continue. And we should let's
say print out ...
... Hello new value for key ...
... and that is to plist ...
Oh, we don't have access to that, let just say 'Hello New Word', okay. We will
check if the save was actually made later on. So let's just build and run
this and ... value ... if I tried it. Lets build and run again and let's see
if that went okay. Okay! Here it is an action has been
performed and here is saved successfully value, new value and we have a lot more
inside SwiftyPlistManagers log. An action has been performed you can check
if it went okay by taking a look at the current content of the Data.plist file.
And here we have newKey and Hello New Value. Now this is printed out and it is
saved I promise we will maybe take a look at it later and get the value and
print it out but if you take a look at the Data.plist file you see that
nothing has changed and that is the way it should be because changes are made
with inside the data, the document directory of our Xcode project.
So let's go back here and let's that's just yep
I will leave fatching values to the for the next video, okay. Hope you liked this
video. If you liked it give it a thumbs up if you didn't that other button seems
to work okay as well. And if you want to see more of these kind of videos you
could just simply hit the subscribe button down below and I will see you in
the next one.