Swift programming: Classes
We’re going to talk about class
and you can think of it as a way to organize your information. Now I know we talked about functions
and that was a way to kind of group pieces of code together, when you think about classes
you’re more thinking about information.

For example, in this scenario, I have a name
, salary
, role
. All of these different variables
or pieces of information have to do with a person or an employee or something like that, so what we can do is we can group this information together into our own new data type called an Employee
, and in order to do that we define a new class
called Employee
. Let’s take a look at the syntax involved.

So you should start with the class
keyword followed by a space and then your class name. In this case, we would call it Employee
followed by space, and then you have a pair of curly brackets{} inside. Inside your curly brackets is where you would define your class
.
Let’s jump back to the playground and define our Employee
class
:
One thing to keep in mind is that you’re not defining the information for a single
Employee
because remember we are creating a new data type here calledEMployee
. So this is a general definition that you can use over and over again instead you’re specifying what sort of information everyEmployee
should have.

So we don’t fill out the specifics now, we just say that an Employee
data type should have a name
, it should have a salary
, we could start at zero and fill it in later. And let’s say for the role
it should be a String
type as well and by just specifying two quotes with nothing in between them, it’s just an empty string so just like that you’ve defined your new class
which is essentially a new data type its called Employee
, every single Employee
is going to have a name
, a salary
, and a role
.
A class definition is like a template or blueprint, when you create an actual tangible instance of the class, that instance is called an "object".
Let me show you some examples:

Since we’ve created a new Employee
object and assigned it to C
, we write C
which points to our Employee
object, and we hit dot on our keyboard and that lets us access all of the data inside of the Employee
class. You print(C.salary)
and you can see that it says a 1000 right there:

I can also set the other pieces of data like that, also define a function doWork()
in here:

How would I execute this function?
If I just try to call the doWork()
like this, it’s not actually going to be recognized because this function is part of my Employee
class. I would actually have to call this function on an object of that class.

I can actually do C
using dot notation now and you can see that I can access this function actually, why don’t we change our print statement here and insert the name
.
Now I want to show you something else, so I’m going to define another Employee
and create another Employee
object in memory and I’m going to set this process name to Karen
.

Now you’ll notice that c
and d
are two separate Employee
objects when I’m modifying the data for d
it doesn’t affect c
, and when I call the doWork()
on d
it has nothing to do with the doWork()
for c
.

One last thing I want to mention is that when you declare a function
inside of a class
, it’s actually called a method
of that class
and these variables
that you’re defining inside your class
are called properties
of that class
.
This content has been adjusted by individuals and is only used for personal notes, please do not plagiarize and commercial use.
Reference —