Packages and Modules
Access Boundaries
Access control keeps stored details behind public methods and properties.
Hide state behind methods
access_boundaries.swift
struct ScoreBox {
private var value: Int
init(start: Int) {
value = start
}
mutating func add(_ amount: Int) {
value += amount
}
var current: Int {
return value
}
}
let bonus =
var box = ScoreBox(start: 10)
box.add(bonus)
let message = "score=\(box.current)"
print(message)
struct ScoreBox {
private var value: Int
init(start: Int) {
value = start
}
mutating func add(_ amount: Int) {
value += amount
}
var current: Int {
return value
}
}
let bonus =
var box = ScoreBox(start: 10)
box.add(bonus)
let message = "score=\(box.current)"
print(message)
struct ScoreBox {
private var value: Int
init(start: Int) {
value = start
}
mutating func add(_ amount: Int) {
value += amount
}
var current: Int {
return value
}
}
let bonus =
var box = ScoreBox(start: 10)
box.add(bonus)
let message = "score=\(box.current)"
print(message)
access control
`private` keeps implementation details inside a type while methods expose the operations callers are allowed to perform.