When a constant, variable, property, or subscript makes use of a private type, the
constant, variable, property, or subscript must also be marked as private:
private var privateInstance = SomePrivateClass()
Getters and Setters
Getters and setters for constants, variables, properties, and subscripts automatically
receive the same access level as the constant, variable, property, or subscript they belong
to.
class Samplepgm {
private var counter: Int = 0 {
willSet(newTotal){
println("Total Counter is: \(newTotal)")
}
didSet{
if counter > oldValue {
println("Newly Added Counter \(counter - oldValue)")
}
}
}
}
let NewCounter = Samplepgm()
NewCounter.counter = 100
NewCounter.counter = 800
When we run the above program using playground, we get the following result:
Total Counter is: 100
Newly Added Counter 100
Total Counter is: 800
Newly Added Counter 700
Access Control for Initializers and Default Initializers
Custom initializers can be assigned an access level less than or equal to the type that they
initialize. A required initializer must have the same access level as the class it belongs to.
The types of an initializer's parameters cannot be more private than the initializer's own
access level.