Skip to content
Justice Tech School

Access Control

Keywords private, fileprivate, internal (default), public and open control who can use a member. Keep implementation details private.

Example
struct Account {
    private(set) var balance = 0
    mutating func deposit(_ n: Int) { if n > 0 { balance += n } }
}
var a = Account(); a.deposit(50)
print(a.balance)

Exercise

Hide from outside.

 var secret = 1

Quiz Test

What is the default access level?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.