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
Create a private method named getSecretRevenue()
that returns an Int
value equal to 100.
Assign the sum of (paperclipSales * paperclipCost)
and getSecretRevenue()
to totalRevenue
.
Call the getSecretRevenue()
method on the alphaOffice
instance. You should see an error in the terminal.
Comment the offending line out so that our code will run again.