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

Taking Birthday Note

Contents

keyboard_tab
Advanced iOS Features
1
Introduction
PREVIEW31s
2
Storing Data
PREVIEW11m 37s
5
Second Screen
PREVIEW8m 59s
6
Segue
PREVIEW8m 6s
12
Timers
14m 39s

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
1h 57m
Students
8
Description

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.

Prerequisites

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

 

Transcript

Hi. In order to save our data in this small database that I have been talking about, we're going to learn about UserDefault defaults in this lecture. So far, we have been getting the information from the user but we haven't been able to save them. So, we're gonna use UserDefaults. As you type UserDefaults, it shows you the description that this is an interface to use UserDefaults database. So, this kind of a small database that you can store some single value. Okay. And if you read the description it will store this in a key-value pairings like we did in the dictionaries. So, it's not very practical when it comes to large set of datas, but we're gonna work with small ones. And I have to say standard after this, and this will create the shared default object for me.

So, whenever I call this UserDefault.standard, it will return me the same object. We're going to learn what an object is in the object oriented programming sections, but right now, know that when I create a string for example, it creates an object. Okay so, I say let myString is something and let myString 2 is something else. So, I'm creating different kinds of objects.  Okay. So, they have the other different values, they are different objects like this. So, this is myString2 and this is completely different object from myString. But wherever I call UserDefault.standard, I get the same object. So, I can reach the same data that I have been working on. And after that you're going to say set. And as you can see it asks for a key and a value. So, we can stir URL, brilliant double float integer string. We can store whatever we want in here. All we have to do is just come up with a value and a key like in the dictionaries. So, choose any of this and for value I'm going to save nameTextfField.text, for example. 

Okay. So, nameTextField.text and you can make this force unwrap because we know there's going to be a text. And in the key, I'm going to go for a name, and it doesn't matter what I write in here. I can just write James over here. I can write my own name until over here. But you have to make sure that you don't forget this key because we're going to need it later on when we tried to call this toward value.  Okay. So, make sure there's something logical, make sure this makes sense to you. So,I'm going to go for a key name and of course we're going to have to call and do the same thing for birthday text as well. So, I'm going to set the value as birthdayTextField.text with forced on rep and the key will be "Birthday.

So, I believe that's okay. And after this maybe you see some documentation on the internet or some other researches. Maybe they're trying to call this UserDefault.standard.synchronized, But this is an old method.  Okay. You don't need that anymore. So, I'm not going to do that. Maybe I can just leave it as a note, but you don't have to call this right now. It will just store this data without having to do anything else. So, this will actually save the data.  Right. We're going to save this data in a database, but without having to call this without having to get this data from database, we cannot show it to the User. And we have to do this in the viewDidLoad. Why? Because after the user closes down the application and opens it again, viewDidLoad will be cold. 

And that's when we want to get this data and show it to the user, because once I click save it will display the name and birthday labels anyway, but when I reopen the app the birthday and name labels will be empty. That's why I need to get this data in the viewDidLoad to display them in the name label and birthday labels. So, let me create a variable called short name. And in order to get this, I'm going to call UserDefault.standard.object, and when I say object it asks for a key and it gives me an any object as you can see. So, remember any, we don't know this type.  Okay. We don't know whether this is a string or this an integer. So, we have to cast this as a string after we get this. So, this is any optional. Okay . And remember our "name" is "name". So, this will check to see if there is a name, it will give it to me.

But how can I cast this to be a string? Because right now, I cannot just say nameLabel.text is short name, we're gonna see how to do that. So, let's start birthday UserDefault.standard.object for key birthday. So, I'm doing exactly the same thing and I'm using exactly the same keys over here. So, suppose that I get storedName and storedBirthday. So, can I come over here and say nameLabel.text is storedName. I cannot say that because it's any right now, any option of. And I don't want to do it that way anyway, because once I do that, I will get this error. So, let me click on this, and as you can see it says that you cannot convert this to string optional unless you do something. So, what are we going to do?

Can I do a casting like we did before with as. But also there are two types of castings as? and as!. So, how do we do that? Can I say as? string. So, this will attempt to convert this to a string and if it works it's fine and if it doesn't work, it won't crash.  Right. So, this is good but if I do it like this, it will definitely try to convert this into a string and if it fails, the app will crash. So, remember when we used this exclamation point we need to be very careful and we need to be certain of ourselves. So, I'm going to go for even more robust, more safer way to do this. I'm going to control this to be a new variable with if let.

Okay. So, we have seen this before.  Right. If we actually come across the situation like this where we have to decide between optional and force unwrapping, you can use if let. So, I'm going to say if let myName = storedName as? string. So, this is very good, then in here, I can just take this myName and let it be my new nameLabel.text.  Right. because if this works, it means that storedName will be cast the string and myName can be myTitle. Right now, we face if I say myName this is not an optional, this is just a string. This is not optional string. If this works, it will display. If it doesn't work, it won't happen to be crashing or it won't display anything. So, let me just change this to newName because we were going to have a newBirthday or here as well in order to avoid any confusion, I'm just going to use new. So, storedBirthday as string, and this will be the same thing. birthdayLabel.text is newBirthday.

So, this is cool.  Right. If it works out then it's okay. If it doesn't work out, don't do anything else. Just don't crash my app. So, let me run this and see if we can get the same results. So, we don't get anything right now because we haven't saved it yet. So, let's go for Dave and Jan 20.  Okay. And let's close this down and open it one more time. And let's see, if we can see the results. Here we go, we see the Dave and we see the birthday in here. So, right now we have saved this into  our database and we managed to get that information from that database and we're displaying the result to the user.

So, that's how you work with UserDefaults. It's very practical. With a simple line of code we managed to save the stuff and with a simple line of code we managed to get the stuff back from database. And when it comes to big set of data, this doesn't make sense because you have to create so many variables, but it's much easier to work with this if you're looking for saving one data or two data, then this is perfect for you. Okay.

And if you're looking for saving like 1000 data points then this doesn't even make sense to you. And again, we have learned about casting in here.  Okay. So, first casting an optional casting and the best way to do that in here is to do this with a optional casting with if let. And we have to learn one more thing. We don't know how to delete this data from this database. So, I'm going to replace this Dave or I just want to delete him from my database. How do I do that? We have to learn about that as well, and we're going to do that within the next lecture.

About the Author
Students
431
Courses
55
Learning Paths
3

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