Saving Highscores


The course is part of this learning path

Start course

In this course, we're going to create a game for iOS in which you chase a character, and each time you tap him, your score increases. This will challenge you to think about how you would build your own game using your existing knowledge of iOS and then, of course, we're going to code along together to create the game.

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills


To get the most out of this course, you should have some basic knowledge of iOS.


Hi. Within this lecture, we're going to finish our app by taking care of high score storage. Right now, we don't have any high score check. So, we have to do that when the time ends. So, I'm going to call this HighScore. So, that's where we're going to take care of this. Why?

Because let me see, we have a High Score Label here, in which I want to show the highest score that has ever been made in this game. So, maybe using that, we're going to do this with user defaults. And that's correct because we're only going to save one little value, and that's the perfect way to do it. So, we're going to control if score is higher than the highscore, if current score is actually higher than the high score, then I'm going to save this new score as a high score. If it's not, then I'm not going to change high score at all. So, this is how it's going to go down. We're going to check to see if our score is actually higher than the highest previous score. And in the viewDidLoad, of course, we're going to check to see if we have stored any high score, and we're going to take it, and show it in our High Score Label.

So, we need a high score variable in order to do that. So, I'm going to say, var highScore = 0. And we're going to take this from the viewDidLoad, from the user default, so it's actually easy to assign this to be zero initially, and then we can check to see if we have any other scores stored. So, when the time ends, I'm going to check to see if self.score is actually higher than self.highScore. So, if that's the case, then I'm going to need to just store this value as a Highscore. So, first of all, I'm going to make sure that my highscore integer, my self.highScore integer is now self.score. So, this will be updated. But also, I'm going to make sure that I'm saving this highScore variable inside of my user default database. So, you can just go and say, highScoreLabel.text is now "Highscore: \(highScore)"  or \(self.highScore) So, this is good. This will display this HighScore label to the user. But also, we want to store it, so that we won't lose it once the user closes down the game. So, UserDefaults.standard.set. You remember how this works? You have to come up with a value and with a key. So, our value is simple here, self.highScore. 

Because we already decided it to be the highest score. And then forKey, I'm going to use highscore again, but we can call it anything we want. So, I'm going to go for highscore. And we need to remember this key, because we're going to need it in a minute. So far, so good. Let's go to viewDidLoad. In the viewDidLoad, we have to check for high score as well. Because there will be some cases that we store the high score, and there will be other cases we didn't score the high score. For example, for the first time a user opens this game, there won't be any high score saved. So, I'm going to say, let storedHighScore, and I'm going to get this value from UserDefaults.standard.object. And forKey, of course, I'm going to use the same key that I have stated. So, this is "highscore". And I'm going to check to see if this value exists or not. So, I can come over here and just say, if storedHighScore == nil.

So, if that's the case, then it means that I didn't start anything. So, I can just show high score as 0. I can say, highScore = 0 and highScoreLabel.text is actually 0,  or "Highscore: 0". Or let me do it this way, this will be much more clear, so \(highScore). OKay. So, this will display the highScoreLabel.text. And it shows me as an unresolved identifier because I misspelled the high score. So, this is good. But if that's not the case, this won't be even run. This won't get executed. 

So, I can just check to see, just try to cast this as an integer. So, I'm going to say, if let newScore = storedHighScore as? Int, optional integer, casting optionally as integer, that is more correct. And if I can do that, it means that there is a new score. And it means that I store the value and I can cast this as an integer. Now I can just say, highScore = newScore, and highScoreLabel.text is again "Highscore: \(highScore)". This time I'm going to spell it right, so highScore. This is good. So, it says that new score is not used, but that's not the case. the warning went away. So, we're good to go actually. So, let me test this to see if that works. So, there is not a Highscore value stored right now.

And as you can see, Highscore is shown as 0, so let me try to tap on Kenny if I can. Yes, score is 1. Apparently, I'm not very good at this game. So, as you can see, Highscore is shown as 1. So, let me try with another Highscore. Hopefully I can tap on Kenny. I did a score of 4. So, let's wait, and here you go. Highscore is shown as 4. So, let me close this and open it one more time, and let's see if we can see the 4 as high score. So, here we go. As you can see, we managed to save this information in the user defaults. So, our game is definitely working, and we finished this game actually. And be remind that this game is created by me couple of years ago, and you can find a lot of clones of this game in the App Store and Google Play. 

And it's absolutely fine to do that. Just try to find an image that will not cause any royalty problems. So, you can google for royalty-free images. And then, just try to add some more features to it. For example, a lot of students have actually implemented some new levels, making it harder to tap on Kenny. So, a lot of students actually used cloud servers to save the high scores, and try to make a leaderboard or something in the game. You cannot do that with your current knowledge by the way. After you complete the course, try and improve this game a little bit further. So far, so good. We're going to stop here. And within the next section, we're going to learn about something called TableView.


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