• 20 hours
  • Medium

Free online content available in this course.



Got it!

Last updated on 5/25/20

Create your first class

Log in or subscribe for free to enjoy all this course has to offer!

We've started with the design (which means identifying the properties and methods that describe our object). Next comes implementation.

Get aboard!
Get aboard!

Next thing to learn is the concept of class. A class is a description - a blueprint of an object. And in familiar terms a class is a type of object!

So, basically, as developers we can create custom types (or named types) - as many as want :zorro:.

Let's proceed with the implementation of our design, which means translating the description into the code.

And where are we going to do that?

To write our code we are going to use Playground!

Settling in Playground

I recommend creating 2 playground projects for this course:

  • OOP.swift: to play with code samples while learning new stuff

  • NeXTDestination.swift: to work on the project progressively during the course

We are going to use 2 virtual desktops to conveniently switch from one file to another.

Declaring a class

To implement our object design we need to declare a class. For that we use the  class   keyword followed by a name of a class and finishing with the curly brackets that will later contain all of the classic logic. Here's the Swift syntax for a class declaration:

class ClassName {
// Class logic

We can use any name we like, however, as always, it’s best to use descriptive names.

For example, if we are creating a class describing a car, we don’t have to reinvent the wheel, we can just call it  Car ! If we are creating a class describing an smashing gesture, we could call it  SmashingGesture   or  DontTryThisAtHomeGesture . Just kidding, this gesture doesn’t exist (yet ;)), so don’t try to smash your phone :p

Following our NeXTDestination designs, we've got to create 3 classes - Entertainment, Destination, and Adventure:

class Entertainment {
class Destination {
class Adventure {

They contain no logic for the moment, hence, hardly useful. It won't be long until they turn to magic :magicien:!

Introducing instances

We might have reached a bit of a confusion point here.

First we designed our object (that included identifying properties and methods we talked about earlier). Then we declared a CLASS - which is a TYPE of an object - a blueprint that implements the original design.

So, how do we get to actual objects?

Objects are instances of a class. They are variables of a class of data types. Classes are like molds for industrial manufacturing process.

We create an object, an instance of a class, when we create a variable of that class:

var instance = ClassName()

Instances are actual virtually tangible objects manufactured from a mold.

You've created instances already, however not yet of class types but similar:

var myString = String()
var myArray = [String]()
var myDicitionary = [String: Int]()

And what about other types?

Other types already known to you, like Int, Float, Double, Boolean are not classes, however they share many concepts of classes, including the initialization: 

var i = Int()
var f = Float()
var d = Double()
var b = Bool()

Indeed, very similar.

If those are not classes, what are they?

We will discover some more details about types later in this course :soleil:.

Let's Recap!

  • To declare a class in Swift, use the following syntax:

    class ClassName {
    // Class logic
  • A class is a named type. When we create a class, we create a type.

  • A class represents a blueprint. Concrete representations of this class are called instances. To create an instance, use the name of the class followed by parenthesis:

    var instance = ClassName()

Example of certificate of achievement
Example of certificate of achievement