Hi, Alex here from rebeloper.com and in this plist file tutorial I will show you
how to fetch, add and save new key value pairs with the help of Swifty Plist Manager.
So without further ado let's dive in and let's see how we can fetch some values.
In the last video you have seen how to get those values and
these values were unwrapped in the completion handler. Now sometimes and
most often that just sometimes you would need those values outside of a
completion handler. So let's do that. So I will just guard let with a guard let
statement I will just unwrap it. So let's say ...
and let's say our result equals and that is SwiftyPlistManager.fetch
sorry about that shared.fetch and here we have our
fetch value and for and what should be the string that should be newKey and
from our plist with name and that is DataPlistName.
Great! And now else we should return.
Okay, now let's just print this out and I will just copy this out for the brevity
of this tutorial. So the value for key, new key is result from the plist DataPlistName.
Okay let's just build and run and let's
see what happens. And here we have it the value for key newKey is Hello New Value
from the plist, Data.plist. And the awesome part is that now you can use
this constant this result down the line, okay. And that is how you can fetch these
results instantly into variable or constant, okay. Next up is adding a new
key value pair and let me just go here. Let just comment all of these out. So the
log will not be as busy. So at the new key value pairs.
SwiftyPlistManager.shared. add new yes we didn't cover add new.
So add new and for the value it should be let's say
rebeloper value. And for the key it should be the first key.
Okay, and into the plist name that
is DataPlistName, okay. And for the completion, oh sorry about that
not there we should go into the completion... we have an error here and we
should check if that error is not equal to nil and if it does
equal to nil them we don't have an error. And now we should just print out
and I would just add here the newKey value pair and the key is first key.
And the value is rebeloper value. ...
... inside of ...
... that is a really nice print statement.
Okay let's build and run and let's see what happens. Okay an action has been
performed you can check if it went okay and here we have our first key and it
equals rebeloper. That is a great. And here we have our print statement
successfully added new key value pair and here is our first key and the value
is rebeloper inside the Data.plist file, good stuff here. Now of course you
want to add new values but you also want to save let's say update those values.
So before we do that let's let me just tell you that we can add as many types of
values, here I have int value, a bool value and we could add that for
the second key or the third key. I will not do this for the brevity of this
tutorial but go ahead and replace this rebeloper value and the first key
with these ends and bools and you will see and the bools will be represented
with 0 for false and 1 for true. Go ahead and this is your homework for learning
adding new key value pairs with the help of Swifty Plist Manager. So let's move
on and update the values for our keys. And I will update the value for our
first key and that is with the save function. So SwiftyPlistManager.shared
and type in save and here we have our value and for the key to plist
with name and a completion, okay. And I we'll save a new value and I will save
this string value, okay. And for the key and that is the first
key of course because I want to change that I want to update that and into
plist with name and that is the DataPlistName of course and in the
completion we have an error that we need to check out. So if error = nil then
we should print out successfully saved and let me just print this out and
change the successfully saved newKey value pair. And this is for the string value.
Sorry about that for the first key here is the our string value inside of the
Data.Plist file. So let's go ahead and build and run again and let's see if
the change has been taken in effect, okay. Let me just scroll this up so you may see
the code and here we have it. Successfully saved newKey value pairs,
first key and it is Alex inside the Data.plist file. And as you can see
here I have printed out the Data.plist file and for the first key we have Alex.
Of course you may also check with the fetch function of our SwiftyPlistManager.
Okay that concludes adding new values and saving new values and for
just a little fun let's just add and instantly save a dictionary, okay.
SwiftyPlistManager.shared and let's say save and the value should be
this dictionary that I have created with the name and an age, okay. Here we have
our dictionary and the key should be let's say our
fourth key.
Okay let's just put it there and of course it should be inside the Data.plist
file, okay. And here we have our completion let's just hit return on that
and let's check for errors. If error = nil
then let's just print this again well we will not print anything out. Okay? Let's
just print out ...
Okay it will be printed out anyway this SwiftyPlistManager Data.plist
file will be printed out. It was double equal sign, okay.
And now let's just build and run and test it out if the dictionary has been
saved. Here we don't see our value. Where is our dictionary? And that is because we
didn't add our dictionary. So we need to add our dictionary first.
So SwiftyPlistManager.shared.addNew value and that is the dictionary and for
the key that is fourth key to plist name DataPlistName with a completion
handler that is the error and I will just check for errors. If error = nil
then print out ...
... dict added, okay. And here I will just say dict saved or rather updated.
And for that I will just change the dictionary. So inside the
dictionaries name value or maybe the age value I believe,
yes. The age value we should add dict... sorry about that. It should be let's say 35
One year older so the original is 34 and now we want to save a
new value. Of course you need to save the whole dictionary inside our Data.plist file
So here we have it, okay. And of course the age has been saved and
if we just scroll up a little bit at the beginning when we edit our dictionary
and then we had the age at 39. Good stuff here. And now you know how to
fetch add new key value pairs and save/update those values in the key
value pairs with the help of Swifty Plist Manager. Hope you enjoyed this
video I will see you in the next one where we will remove some key value
pairs and the whole structure of the Data.plist file and some error handling.
Till then see you in the next one and if you liked this video go ahead and give
it a thumbs up. And if you liked this video and want to see more go ahead and
click on the subscribe button down below it seems to work okay and I will see you
in the next one!