1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Advanced iOS Features: Permanent Features, Segue, Alert Message, Timers, and Gestures

Storing Data


Advanced iOS Features
Storing Data
PREVIEW11m 37s
Second Screen
14m 39s

The course is part of this learning path

Storing Data
1h 57m

This course explores some advanced iOS features. We'll look at how to store data permanently. You'll learn how to work with more than just one screen, how to use Gesture Recognizers, Timers, Alert Messages and so much more. You're going to have essential skills for iOS development after you complete this course.

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.


To the most out of this course, you should have some basic understanding of programming and computer science in general.



Hi. Within this lecture, we're going to learn how to store data in the phone's memory. So far, we have been collecting input from the user. For example, users gave us some numbers in the calculator and we know how to get them within text fields or some other views but we don't know how to store them. What happens when the user closes their phone and what happens when they open it back?

If we don't store the data in a small database, then we would lose it. So, we're going to learn about a good technique so that we can actually save small information into small database so that we can reach that value whenever we need. I'm going to create an Xcode project. For that, I'm going to go for a Single View App and we're going to build a Birthday Note Taker. So, I'm going to call this BirthdayNoteTaker and again we're not going to use SwiftUI. We're going to go like this. So, hit Next and places on your desktop or any folder you want. So in fact, this app will let us only store one person's birthday as a note. So, this is not very useful because we're going to see how to work, how to store a small amount of data right now. Later on within the course, we're going to see how to use large databases to save large datas into memory and then we can reach them whenever we want.

But right now, we're going to learn a very practical technique so that we can store datas and we can just reach them whenever we want. For example, we can store high score datas. We can store, I don't know, username information in order to reach them when the user closes their app and opens them one more time. So, I'm going to keep it simple. I'm going to ask for a name and a birthday. So, I'm going to have a Save Button over here and I'm going to leave, I'm going to put two labels on the below so that I will show the stored information in those labels. So, it's going to be simple. Maybe, you can try to do that on your own as well. So, two text fields button and the label, just do it and connect everything to the ViewController.swift. I hope you managed to do that. So, it's fairly easy. We're going to use text fields so let me open the library and search for Text Field we have worked with this before. So, I'm going to drag it and drop it over here and I'm going to make this a little bit bigger so maybe our friend has a long name or something. So, let me go to Attributes Inspector and give you the Placeholder. So, first we can ask for name for example, and I'm going to copy this with command C and I'm going to pass this below with command V and you can do it from the library as well but copying and pasting actually makes it very easy.

So, in the Placeholder this time, I'm going to go for birthday. So birthday, we asked for a name and we asked for the birthday. Right now, I'm just going to add some button to save this information to our database. So let me go and do this, button, so let me name this button Save and let me make this a little bit bigger. So here we go, Save. And again, we will have two labels in which we will display this sort data. So, search for label and put it over here. And make this a little bit bigger and put it in the place, center place and I believe that's good. Maybe we can make this a little bit bigger. So let me make this in line centrally and for label I can go like this. I can just say Name: . I'm going to write name in here. So, let me copy and paste this and we're going to display the birthday in this label again. By the way, of course we could have just displayed the stored labels, stored text in here as well, but I just want to display this in an obvious way so we don't lose track when we try to learn about this saving into small database. So, this is okay. We have what we need in here. We can just go ahead and try and connect every one of these buttons into buttons and other views into ViewController.swift.

So, hit Viewontroller.swift in here and we already have made that storyboard over here. So, I'm going to hit cntrl on my keyboard and drag and drop to this class and I'm going to call this nameTextField. I'm going to go and open some new lines over here and again do the same thing for Birthday. So, birthdayTextField. And we have to define our labels as well so let's go for name Label. This is nameLabel and lastly the birthday label, show birthdayLabel. I hope you manage to do that on your own by the way. So, let me just take this action button and just saveButtonClicked or saveClicked. So, this is okay. We have connected everything and before we forget, let's add some constraints to here saying, Reset to Suggested Constraints, so they will be in the right place. And let's run this actually and see if everything seems to be in order, if not we can come back here and do some changes. It's always wise to do that before we go to ViewController.swift. And again, here in the Save button, what we want to do is actually, we want to save this information into a very small database like structure but we don't know how to do that yet. Before we do that, I'm just going to go and display these values in the nameLabel and birthdayLabel anyway because we're going to do that later on. We're going to need it eventually. 

So, it works fine by the way. So, we don't have to change anything with the view. So, let me tidy up here a little bit. So, I think we are good to go. So, how do I take nameLabel and show the nameTextField's value over here. So correct, nameLabel.text, nameTextField.text. So, I'm taking the nameTextField.text and make it equal to nameLabel.text. So, we're going to do that for a birthdayLabel. You know how to do that. We have worked with this kind of thing before. So, birthdayTextField.text. So, all this app will do right now, I will write something in the name and birthday text fields. It will display them over here. But again, it started with name in our example. So, how do I do that? I don't want to just show the nameTextField, I want to show it like this Name nameTextField. And if I come over here and if I try to say something like this birthdayTextField.text, it doesn't work because it thinks that this is a string. It is a text and this is in fact, a text. In order to treat this as a variable, I have to use a special sign text in this "". And in order to achieve this, we're going to use a \ in here. And then ( and then ). So, between here I'm still writing a code, I'm not writing any text actually, I'm just writing my variables.

So, this will be displayed like Name and then nameTextField.text. And again, in here, so this will be "Birthday:\ (birthdayTextField.text)". So, you can use this \ and then ( ) in order to use variables inside of strings. So, we have to make this force unwrap and we're pretty certain that this text is going to give us something because even if user doesn't give anything, it will return an empty string, not a nil value. So, this will be okay to force unwrap, remember the optionals and remember the calculator thing that we have built. And if you haven't watched those sections, I suggest you go back and do that. So, I believe we are ready to test and see if this works fine. 

Let me open this in stimulator one more time and let's see if we can get the values and display them in nameLabel and birthdayLabel. So, let's say Dave here and for the birthday, I'm going to go for Jan 20 and if I say Save, then I see both Jan 20 in the name and birthday because I accidentally put birthdayTextField.text in here. Maybe you have realized my mistake when I was writing this code. So, maybe you already have corrected this. So, this should be nameTextField.text. So, let me call this one more time. And in fact, you will see that this day when Jan 20 went away. We cannot see them right now. And I hit the same button, I see this over here but once user closes this down and opens it again, this information will go away. So, how can we make certain that this information will stay here? That's right. We have to write this into a very small database-like structure and that's what we have been trying to learn about and we're going to do that within the next lecture.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics