Learn

There are some cases in which we may not want to expose methods outside of the scope of the structure such as when the method might access or modify some sensitive data. For those cases, we can use the private keyword to define a private method, in the same way we defined a private property:

struct WizardCat { private func secretSpell() -> String { "Abracadabra" } }

As we saw in the previous exercise, now only methods inside the scope of the structure can call secretSpell(). If we try to call the function outside of the scope of WizardCat we will encounter this error:

error: 'secretSpell' is inaccessible due to 'private' protection level

Instructions

1.

Create a private method named getSecretRevenue() that returns an Int value equal to 100.

2.

Assign the sum of (paperclipSales * paperclipCost) and getSecretRevenue() to totalRevenue.

3.

Call the getSecretRevenue() method on the alphaOffice instance. You should see an error in the terminal.

4.

Comment the offending line out so that our code will run again.

Take this course for free

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