Reputation: 42622
I have a simple Vehicle
class which has a private field named _odometer
, then, I have a computed-property named odometer
(it has setter & getter).
I think _odometer
should not be able to set since it is a private field, but in playground, it can be set, why? Here is my code in play ground:
As you can see, I created an instance of Vehicle
named volvo
. By default _odometer
is 500, but I can directly set _odometer
to 0 on volvo
, when print out the 'odometer', it is 0. WHY I can set private field?
Upvotes: 1
Views: 80
Reputation: 36401
Swift private
is slightly semantically different than in other OO languages, it is private to the source file.
Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.
Upvotes: 1