What I found until now is the scenario of a public get and private set like visible below.
private(set) var distanceTravelled: Double
I want it the other way around. Of course the following is not working.
private(get) public var distanceTravelled: Double
public(get) private(set) var foo:String. to be doubly explicit. The goal is that foo should have a getter which is accessible from outside the module, but a private setter. Using only private(set) means that the getter is internal - so not accessible outside the module.
The reason for declaring the getters and setters private is to make the corresponding part of the object's abstract state (i.e. the values) private. That's largely independent of the decision to use getters and setters or not to hide the implementation types, prevent direct access, etc.
To create computed properties, Swift offers you a getter and (an optional) setter method to work with. A getter method is used to perform a computation when requested. A setter method is an optional method. It can be used to modify a related property.
If you want a public setter but a private getter for this var you can declare it as private:
private var distanceTravelled: Double
and create a public method for setting this variable:
public func setDistanceTravelled(distanceTravelled: Double) {
self.distanceTravelled = distanceTravelled
}
I don't believe this is possible. To quote from the documentation:
You can give a setter a lower access level than its corresponding getter
That is, you can only alter the access in one direction, and that is to make the setter more restrictive than the getter.
This is possible, as of Xcode 10.2 / Swift 5.
You combine a computed property with an @available
attribute, like this:
public var distanceTravelled: Double {
@available(*, unavailable)
get { internalDistanceTravelled }
set { internalDistanceTravelled = newValue }
}
private var internalDistanceTravelled: Double
Note that if you make distanceTravelled
visible to Objective-C, the unavailable
will not extend there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With