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

Deleting Data


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

The course is part of this learning path

Start course
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 see how we can delete this name and birthday information that we have saved in the userDefaults. So far, we have been saving this and it works, right? Now, I will add a Delete button to my main storyboard so that when a user taps on this button our data in the userDefaults will be deleted. So, I am going to add another button over here. Right next to do Save' button and I'm going to name this Delete. So, let me make this a little bit bigger. And then, align it and I believe that's okay. So, right now I'm going to go for the constraints and we have given constraints but now we added a new view. So, we have to give one more time. 

So, I'm going to say 'clear constraints'. I'm going to go over and say 'Reset to Suggested Constraints'. Of course, you could have just said 'Add Missing Constraints' as well. So, let me open the assistant editor over here and choose ViewController.swift. And in here, I'm going to take this 'Delete' button and define it as an action in my ViewController.swift. So, hold 'control' and drag and drop. Now, I'm going to call this deleteClicked. So now, we are ready. So, let me close this and let me go to ViewController.swift. So, what do we want to do in here? I want to remove this value from the UserDefaults. And it's actually very basic. So, you have to just go like this UserDefaults.standard.removeObject and you can just give the key like name, like birthday, and that's all. That's all you have to do to delete a value from UserDefaults. But of course, before we do that, I believe the basic safety precautions will be good here. So I'm going to check to see if those values are not existing or not. 

So, let me just define these variables one more time. So, let storedName = UserDefaults.standard.object. And the object that we are looking for has the key name. And we're going to do the same thing for birthday as well. So, I'm going to say storedBirthday. So, this is good. We know this far. Now, I'm going to check to see if this can be casted as a string. So, how do we do that? Of course, we do if let newName, storedName. So, this is optional. And I'm going to try and cast this as an optional string. And if this works out, I'm going to remove this object so I can cut this and paste over here. And the reason that I'm doing this from the scratch and not copying and pasting to show you this. As you can see, it says that you created a variable called newName but you have never even used it. So, if you're not going to use this newName at all, you can replace this line as this (storedName as? String != nil). So, this will do the same thing if I say fix and you will see that if storedName as? String != nil. So, it basically means that if we can do that then, just go and remove this name object. 

So, you know the difference between nil and an empty string. So, we have talked about this. And if it's not nil, it means that I have saved something so I can remove it safely. If it's nil, then I must, I may have some difficulties removing it. I may crash the app and I don't want it to happen. So, I'm trying to understand if this is nil or not. And before we did if let thing because we casted this as a variable and we used that variable. Right now we're not using that variable. So, we can do the same thing as for birthday as well. So, I'm going to remove birthday object from here. So that's it. We're controlling if this exists and we're controlling if we can remove it, if it's nil or not. So later on, I'm going to just say nameLabel.text, name, birthdayLabel.text, birthday. So, I'm going to reset the labels as well. And in here, we're just assigning this to be newName rather, I'm going to go for this name. Reverse, slash and open parenthesis, close parenthesis. 

So, remember that's how we actually use variables inside of strings. Okay. so, I believe now this is consistent and this is going to work. So, let's try this. Let's run this in our simulator. Right now, I have a value stored. Dave and the birthday. So, when I open this, yes, this is now showing up. If I hit 'Delete' button, it will delete these values from here. So, in order to check if we deleted them from the userDefaults as well, I'm going to just close this and run one more time. If we see an empty label, then it's working. If we see Dave again, it's not working. As you can see, we have an empty label over here. So, deleting works. So, now you know how to work with userDefaults in order to save small data in userDefaults database. If you're going to sort some 1000 people or some 100 people in the database, then this is not going to be practical for you. In order to do that, we're going to learn something called Core Data, but we're not going to learn it for until like four or five sections. When we learn it, you're going to be working on real databases. We're going to stop here and we're going to continue our iOS advanced options, iOS advanced structures in 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