Learn

As we learned in the previous exercise, private is the most restrictive level of access control and can be applied to properties or methods. When we make a property private, we make it so that its value can only be read and written within the scope of the structure that contains it.

Here is the syntax for creating a private property in a struct:

struct Cat { private var numberOfLives = 9 }

Recall from the Structures lesson that a struct includes built-in memberwise initializers for properties that allow us to initialize instances without explicitly defining an init() method. However, private properties must have an explicit init() method in order to initialize their value when creating an instance. Otherwise, they must be optional or have a default value.

Here is how we can use init() to create an initializer for the Cat struct:

struct Cat { private var numberOfLives: Int init(numberOfLives: Int){ self.numberOfLives = numberOfLives } }

Since a cat can only lose one life at a time, it makes sense to make the numberOfLives property private, so it can’t be set directly. To enable the desired behavior of the Cat struct, we can implement a method that decrements numberOfLives:

struct Cat { private var numberOfLives : Int init(numberOfLives: Int){ self.numberOfLives = numberOfLives } mutating func loseOneLife() { self.numberOfLives -= 1 print(numberOfLives) } } var scrambles = Cat(numberOfLives: 9)

Recall from the previous Structures lesson that a struct method that changes the value of one of its properties must be marked mutating.

To decrement the private property, we simply call the loseOneLife() method, which has a default access level of internal since we didn’t specify one. This means we can call loseOneLife() on an instance of Cat from anywhere in the module:

scrambles.loseOneLife() //outputs to console: 8

Ouch, sorry Scrambles! Don’t worry though, he landed on his feet 😸

Instructions

1.

Create a private variable named paperclipSales of type Int that is a property of Office.

2.

Now, make an initializer that takes an Int value called paperclipSales and assigns that to the private property paperclipSales.

3.

Now modify the method printTotalRevenue() so that it uses paperclipSales to determine the total revenue of the Office. Assign the product of paperclipSales multiplied by paperclipCost to the existing constant of totalRevenue.

4.

Use the initializer you just wrote to create an instance of Office named alphaOffice, and initialize it with 18 paperclip sales.

5.

Call the printTotalRevenue() method and print out the revenue of alphaOffice.

6.

Finally, create a constant named invalidAccess and assign to it the value of paperclipSales from the alphaOffice instance using dot syntax. You should get a compile-time error because paperclipSales is private!

7.

We don’t want to leave our code broken! Let’s comment out the line with the invalidAccess constant before moving on.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?