Singleton Class
Start course
3h 18m

In this course, we're going to build a Foursquare Clone using Parse. Parse provides you with a great tool to work in cloud servers and you will learn the advantages and disadvantages that come with it.

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 learn a new concept called Singleton Class. So, we're going to see what it is. But what we're doing so far is to 'create' this at place view controller, in which people will choose the name, type, and atmosphere and then they will click 'next'. So, why do we need that singleton? Because when we 'click' next we're going to go to these maps view controller. And we have to transfer this information; the name, the type, the atmosphere, and the image to the maps view controllers so that we can 'upload' them to the parts. Right. And you know how to transfer data between view controllers. You can for example, use prepare for segue in order to achieve this result. But sometimes, there are a lot of data to transfer between view controllers, like in here we have four data but maybe in some cases you will have five view controllers and in which you will have like five different data points. So, You're going to be dealing with 25 data points and it will be harder for you to transfer them. So, we're going to come up with a way to transfer this data with more efficient structure and there are a couple of ways to do that. I'm going to show you one way which is the easiest but it is not preferable due to its nature. Later on, I'm going to show you a lot more clear way, a lot more structural way and we're going to use that and it's called singleton class. First of all, I'm going to show you something called global variables. So far, we have been defining our variables inside of our classes. Now, I''m going to define a variable outside of our class. So, this is above the class as you can see. I'm trying to come up with a way to create global name, global type, and global atmosphere and they are going to be initially empty strings. Okay? Of course, we can do that for global image as well. And the next button is clicked. I can just come over here and say, global name is name text. So, what was it called? Place 'name text', okay, '.text'. So, it will assign whatever is written inside of our place name text to global name variable. And since I defined that global name outside of my class, now I can come to mapVC, I can just call this global name from here. And I can reach its current value. So, let me try this; let me try to print the global name inside of mapVC, even though I define that variable inside the AddPlaceVC as you can see now I called that variable inside of my mapVC. So, let me try this, let me come over here, let me write something like James restaurant or James. Okay. And hit next. And in mapVC as you can see we managed to get that information and write it down. So, this works. You can just do this operation with global variables, but this is not preferable, why? Because maybe you're working with a big team like you have 10 developers in your project and you're creating some variables and if you define all these variables as global variables, someone may just take that global variable without knowing it and change its value somewhere. So, it's not very safe to use that. In this case, we are all alone and we are developing our own project and we are capable of tracking what we have done so far. So, I believe it's not harm to do that, but I'm going to show you a better way to use this. So, I'm creating a swift file. Okay. And the swift file is going to be a class. I'm going to call this PlaceModel. So, we're creating a place model in here and we're going to assign all the properties, all the values that we want inside of a place to this model. And this model will only have one object; one instance, so that we can reach it from anywhere and we can change it from anywhere. I'm going to show you how to do that. So, this is going to be our place model. Okay. And this is going to be a regular class, but before we go on and do that, I'm going to show you what a singleton is. So, I searched Google for singleton class in swift and this came up. So, this is a sample code for singleton class. As you can see it defines the class as a regular array. And it creates something called static let. So, this is a static variable and it can be reached from anywhere. Okay? So, we're going to reach that shared instance, but the key point here is that it uses something called private initializer. Remember, when we use initializers, we define what will happen when an instance of this class is created. So, this is the same thing, but we defined as initializers as private so that this instance of this class, an instance of this class can only be created inside of this class. So, this is kind of not very useful if you want to have a regular class when you want to create more than one object, but if you want to have only one object, if you want to have only one instance then this is great. You can create your own instance from here and people can access this instance and change its value from any class you want. Let me show you what I mean. I'm going to call this place model as I said before, and I'm going to create a static variable. So, 'static let' and I'm going to call this sharedInstance. Okay? So, this is going to be a place model object and this will be the only instance of this class. We cannot create any instance of this class because I'm going to add private initializer inside of this class. Of course, I can choose whatever attributes that I want to assign to this object or assign to this class in general. And people will get to choose, people will get to change these variables, but only for this shared instance. Okay, not for another instance because there will be no another instance. For example, this sample shows us it created some account information. I don't need that. I'm just going to go for PlaceName. Okay? So, my place name will be an empty string and place type again an empty string. Place atmosphere again an empty string and we're going to have someplace image. So, this will be a UIImage. And we cannot do that because we only imported foundation in here; we have to go for UIKit. So, once we do that, now we can initialize the UIImage as well. Now, let me call private init, okay, so that no other instance will be created out of this class. So, this is private init as in here. And I believe I forgot to add this parentheses, yep. Okay, now we are ready to test this singleton class. Let's go to our Addplace view controller and let's go to next button, clicked. I'm going to "Delete" this. We're not going to be using these global variables. I'm going to comment this out. I'm going to leave it here as a note so that you know there is something called global variables, but it's kind of frowned upon to use it in big projects. So, let's come here and call for PlaceModel. So, generally we just do that Placemodel.sharedInstance and we get to reach the attributes when we hit that over here. Right now, we're getting some weird error and I cannot reach the variables. So, let me create a place model over here, so it would be much more clear what I'm doing. So, I cannot do that for example, I cannot say that place model-Place model because it will say that initializer is inaccessible because we have this private protection level. So, rather I have to say shared instance and as you can see, we we get on weird error when we say that, let me do a command B to build this up. We really have an error. In the mapVC, we're still printing the global name, so that was the error that we are getting. So, let's go back to AddplaceVC. And we have commented everything out and let me do another command B to see if we really have another error. Now, we are good to go. Now if I say, '.sharedinstance', we're not getting an error and if I say dot I get to reach the attributes. Now in here, If I say place model, not this place model but this place model that we have created, this one. If I say, place model dot, now I can change the place name. Now I can get it from place name Text.Text. Okay, place name 'Text.Text'. So, again in here, in mapVC, we can call this. Okay, we can call this place model with capital P, PlaceModel.shared name. And you will see that this will give me the same result that I have assigned in the previous view controller because we only have one instance. So, let's go for test and hit 'Next'. As you can see, we managed to print out the test. So, this works. So, our singleton class actually doing its job. So, let's go to Addplace view controller and actually do the other things as well. Before we go on and force unwrap all of this, I want to check to see if place name text is not empty, actually. Place type Text.Text is not an empty string and place atmosphere.Text, this one .Text is not an empty string as well. So, if these conditions hold, then I can 'create' my place model and I can assign all the related values. What do I need? I need actually an image as well. Right. So, I'm going to check to see if chosen image is going to be a place image. Let's see placeImageview.image. If I can get this, okay, then I can be certain that I can call placeModel.placeImage. So, let's do that. name place name text, placeModel.placetypeplacetype Text.Text. Okay, we have one more text. So, this is place atmosphere and place atmosphere Text.Text. Here we go. And finally, we're going to have place model image. So, place model.placeImage and this will be equal to chosen image. Here we go. I believe now we assigned everything that we need. If this works out, we can do the segue. Right. So, I believe we can do the segue inside of the first if. So, if they are not empty we can go over here and perform the segue and else if they are empty we can just show an alert message and I believe we don't have any alert messages in here. So, let me 'create' one very fast. The title will be error. The message will be the place name, place type, and place atmosphere missing, okay, so that user will know they have to give some texts. And here I'm going to say, 'Okay' button is going to be a UI alert action. And this action will have the title of okay, the style of an alert or not alert default and handler will be nil. So, here I'm going to add the action to my real alert. Okay, and I'm going to present this by saying present. And I'm going to present the alert with animation without completion. Here we go. Now I believe we are ready to test this. In fact, we're not going to see if this works or not because we will be transferring data to maps view controller, but we're not using that data right now. Just let me test this and see if we get any error or not. Let's click on 'Next'. We don't get any error and actually we printed out the test one more time. We're going to see if this really works when we 'upload' the data to parse server. Right now, let's stop here and 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