I wish to declare a private variable in a class in Swift and access it with public (actually internal) methods. Is the following code about right?
class someClass {
// Using underscore here to distinguish variable and method
private var _privateArray: [String]
...
func privateArray() {
return _privateArray
}
func setPrivateArray(someArray: [String]) {
_privateArray = someArray
}
}
The reason I want to use the above is because I want to use the array in any subclasses, but with a name which is meaningful to the subclass.
class someSubclass: someClass {
var arrayWithMeaningfulName: [String]
init() {
arrayWithMeaningfulName = super.privateArray()
...
}
}
Not sure if the above is the best way to achieve what I want. Be grateful for feedback.
By the way, I did try declaring the private variable as follows, but the compiler complained:
class someClass {
private var _privateArray: [String] {
get {
...
}
set {
...
}
...