I'm new to Swift and its trying to learn the concept of access control. I saw this code in "the swift programming language 2.1". I understand the code "public private(set) var numberOfEdits = 0"
means that for the variable numberOfEdits, it has a public getter and a private setter.
My question is, can public private(set) var numberOfEdits = 0
be written as public(get) private(set) var numberOfEdits = 0
to clearly indicate that numberOfEdits has a public getter?
public struct TrackedString {
public private(set) var numberOfEdits = 0
public var value: String = "" {
didSet {
numberOfEdits++
}
}
public init() {}
}
No, the getter always has the access level of the unqualified access control label. You can override the setter's level to be lower than the getter's level with either private(set)
or internal(set)
.