Leem.fin
Leem.fin

Reputation: 42622

Why I can set private field in playground

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:

enter image description here

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

Answers (1)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36401

Swift private is slightly semantically different than in other OO languages, it is private to the source file.

In Swift Access Control:

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

Related Questions