• 20 hours
  • Medium

Free online content available in this course.

Videos available in this course

Certificate of achievement available at the end this course

Got it!

Describe classes with properties

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

The first addition to our class logic will be properties.

Implementing properties

We established that properties are variables, so we can create and manipulate them in the exact same fashion. To create a property we simply need to declare a variable within the body of our class: the curly bracket. The syntax of property declaration looks like this:

class ClassName {
    var myProperty = value
    var anotherProperty: Type
}

The major advantage of properties compared to general variables, is that they operate in context of our object.

For example, if we create an object that counts steps taken in a day, one of the properties would be to manage the step count. We would create multiple instances of that class, and, assign them to different people. Whenever we access the step count property for each object, the property will reflect the value of the step count for a person the object belongs to:

class StepCounter {
    var count = 0
}

To access a property using an object variable we use a dot followed by the name of property.

// declare a variable of a class
var myPersonalStepCounter = StepCounter()

// access and modify its property
myPersonalStepCounter.count += 1

// output the result
print(myPersonalStepCounter.count)

Object variable and object property mutability

One important remark to make on the mutability of object/property composition. If we declare an instance of our class as a constant, we cannot modify THAT variable. For example, we can’t assign another object of the same type to it, but we CAN modify properties of that object IF they are variables and not constants.

For example, if our step counter were a constant:

// declare a constant and assign the initial value
let originalStepCounter = StepCounter() // Ok

//attempt to assign a new value - new object
originalStepCounter = StepCounter() // Error

We get an error as we cannot assign a new value to a constant.

Our count property, on the other hand is a variable, not a constant, so we can modify it even if  originalStepCounter   is a constant:

// declare a constant and assign the initial value
let originalStepCounter = StepCounter() // Ok

//attempt to modify a property
originalStepCounter.count += 1 // Ok

Let's now reverse our example. If we declare a property as a constant, we cannot change it, EVEN IF our object is a variable, not a constant:

//declare a class
class UselessStepCounter {
    let count = 0
}

// declare a variable and assign the initial value
var uselessStepCounter = UselessStepCounter() // Ok

//attempt to modify a property
uselessStepCounter.count += 1 // Error

Good to know :pirate:!

NeXTDestination is waiting!

Let's remind ourselves of our original design.

NeXTDestination class design
NeXTDestination object design

And the classes we've declared in the previous chapter:

class Entertainment {

}

class Destination {

}

class Adventure {

}

Try to implement the design on your own. Analyze the purpose of each property and select a suitable type. For this type, implement only the following properties for the respective objects.

  • Entertainment:

    • name

    • cost

  • Destination:

    • name

    • cost

    • entertainmentOptions (available entertainment options)

    • accomplishments (chosen entertainment options)

  • Adventure:

    • amountSpent

    • destinations (available destinations)

    • placesVisited (chosen destinations)

Come up with your own default values for simple types: strings and numbers. And use the type initialization as default values for other properties.

Your turn!

Already done?
Already done?

Ok, let's review the results:

class Entertainment {
    var name = "Broadway show"
    var cost = 150.0
}

class Destination {
    var name = "New York City"
    var cost = 1100.0
    var entertainmentOptions = [Entertainment]()
    var accomplishments = [Entertainment]()
}

class Adventure {
    var amountSpent = 0.0
    var destinations = [Destination]()
    var placesVisited = [Destination]()
}

Looking good!

What's new here is that we are using arrays, elements of which are custom types - Entertainment and Destination.

We already know how to use arrays with Swift type elements and now we added a personal touch - elements of our own class types :zorro:!

Let's Recap!

  • To create a property, we simply use the syntax of declaring a variable within the class definition.

  • To access or modify a property, use the dot notation - starting with the variable name and followed by the property name

  • If a property is a constant, it cannot be modified once the initial value has been assigned.

  • If an instance is a constant, we can modify its properties that are variable, but cannot assign another object to it.

Example of certificate of achievement
Example of certificate of achievement