1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Object-Oriented Programming in Swift

Initializer

Contents

keyboard_tab
Object-Oriented Programming
2
What is OOP?
PREVIEW6m 49s
3
11
Details
9m 33s

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 22m
Students
15
Description

This course delves into Object-Oriented Programming (OOP), covering its main components, and then putting these into practice by walking you through the creation of an app using OOP.

Learning Objectives

  • Learn about classes and how you can leverage them in your code.
  • Learn concepts such as Enum, Inheritance, and Access Levels
  • Understand how these can be used to build apps

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, within this lecture, we're going to create our first project in order to understand classes a little bit more. Rather than going with Single View App, this time I'm going to go for  MacOS. I'm going to create a command line tool. If you remember, I showed you this technique in the beginning of this course before we dive into the playgrounds. So, if you had a problem with playground, there is a high probability that you have worked with this now. But if you haven't, it's not an issue, just create a new  MacOS line command tool and you can name it  MusicianClass. So, that's what we're going to build in this lecture. We're going to create a  MusicianClass and we're going to see almost every aspect of classes so that we can understand them better and we can just implement them in our projects as well. So, this is going to run these codes inside of our  Mac and we're not going to be dealing with main.storyboard or simulators. So, if you just run this, it will print out the result in logs so we can actually follow and track what's going on inside of logs as well. As you can see, it printed out "Hello, World!" Can we do this in playgrounds?

Of course, we can but we're going to learn a lot and we're going to work with different kinds of View Controllers. We're going to see how to work with multiple view controllers or .Swift files in this case, not View Controllers, so that it would make sense to work this inside of command line tool. So, come over here and just say file new, okay? Because we're going to add a new file. So, let's say file, okay? And rather than Cocoa Touch Class, I'm going to go for a Swift file. Because I'm going to create a class in the Swift file and I'm going to name my class Musicians, okay? So, this will be my  MusicianClass, this will be my model actually. So, in order to create a class, it's fairly easy, you can just write class, okay? And then we're going to start with an uppercase rather than lowercase. Remember in classes we use uppercase like this,  'Musicians' and then after you can open a curly brace and it will close it for you. This is your class, right now we actually created our class.

So, we can have some attributes, we can have some actions in this class, right? So, let's start. What can a  Musician have? It can have a name for example, okay? I'm going to initialize this to be an empty string first. So, later on it will have an age, I'm going to say this is zero and it will have an instrument as well. So, you can make this an empty string too. So, suppose that we will have this MusicianClass, okay? And it will have these three attributes.

So, how can we create a  Musician object since now we have our class. It's fairly easy, we can go to our main.swift file, suppose that main.swift is your view controller and we have created a  MusicianClass and now I can create an object out of that  MusicianClass in here. So, first  Musician will be James and if I write  Musicians, it should have show up. Yeah, it sees the  Musicians, as you can see, if I do a command B, it will build up the project or you can come over here to product and say build.

So, the files will be synchronized inside of our XCode project. So, if I write let james or let something else, if I type Musicians as you can see, now it sees the  Musicians class. So, double click on this and as you can see there is a C character on the left hand side, if you do a string, this is kind of creating a string object, like I'm creating a  Musicians object right now. So, if I open parenthesis and close this, it means that I'm creating this object out of the  Musicians class and right now my object is actually created. So, I managed to create an James instance of this  MusicianClass. So, as you can see James has the type Musicians and if I say . it can see the attributes like age, instrument, and name. So, these are the attributes that we have given here. So, let me say property or attribute, okay? So, I can just define the age of James here. So, I can say 50 and I can come over here and say james.name is "James Hetfield".

Okay, and then later on I can define the instrument as well. So, this will be a "Guitar". So, do this for your own favorite Musician and you will see it will work. So, let's try to print age of James. Here we go. We now see the 50, that is cool, right? We have created a  Musician model and we have created an object out of that model and we can set the attributes right now. Now suppose that we're getting this information from Internet, we're downloading from a database like Firebase, okay?

So, if we work on the model, this will be much more structural than ever before. So, we can actually save all those values into arrays, different arrays and we can try to synchronize these arrays using indexes. But if we just create a model out of these values and we can just add those model values to an array, it will be much more structured. And we're going to do that in the following lectures, don't worry. Right now, suppose that I didn't define the values as I did in the James. So, we have those previously defined values, right? So, let me try to print james.age without giving the values. So, what will it be? It will be zero. Because, why? Because I have initialized this to be zero, right? And if I do this print james.name, it will be an empty string.

So, what's the problem about that? So, maybe I don't want to initialize this. Maybe I just want to say this will be an integer or this will be a string. Because I don't want to get number zero when I don't give the age value, okay? So, can I do this? Yes, of course I can, but it will give me an error. So, as you can see class  Musicians has no initializers. So, if I create a property, if I create an attribute without initializing, I will get an error but there is a solution to that problem as well. We have something called initializer and maybe you have heard this as a constructor in other programming languages. So, there is a special function in this Musician class, in all classes actually, it defines what to do when we create this object, when we create an object out of these Musicians.

So, I can do the initialization as well to make this go away but if I don't want to do that, if I don't want to initialize these properties, then I have to create an initializer. So, if you write init, as you can see this is an initializer declaration and this is what happens when an object is created for the first time or second time, it doesn't matter actually when you create an object, it will happen. So, what exactly can we do inside of this init? We can do anything we want. It doesn't have to be related with class  Musicians at all. So, let me do an example so you will understand it better. So, if I initialize this and this and this last one, the error will go away, okay? And I will just print something inside of my init() function. So, I can say  "Musician created" for example. So, whenever I create an object, it will print this log. So, let me create this James, as you can see this is only creating the James and this will only print out the james.name, but before it prints, it says "Musician created."

So, whenever I create another instance of this  MusicianClass, it will again print "musician created." So, it isn't related with properties or attributes at all. Of course, we're going to make it related but let me do this example as well. So, I'm creating another instance from these  Musicians and if I run this, you will see there will be two Musicians created log. So, in init you can write what you want to do when an object is created. So, this is initializer, okay? And again, you may have heard this as a constructor in other programming languages. What we actually want to do in here is to get some name, age, and instrument property values and assign them as our properties, as our attributes. We're going to get those values from the developer in main.swift. So, let's stop here and do that within the next lecture.

 

About the Author
Students
921
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