Swift: Public Properties With Private Setters

In Swift you can easily create public properties with private setters. It makes your code much safer and shorter.

Hint: This post has been updated to Swift 3


Encapsulation means basically that information and states of a class should be hidden from outside of the class – only the class itself should manipulate it. As a consequence, both bugs and logical errors are much more unlikely.

Normally you are using setters and getters to achieve this. However, sometimes you don’t want to provide a setter outside of the class at all. For that scenario you can use properties with private setters.


So imagine we want to create a class that represents a circle. It should be possible to change the radius. Furthermore, both the area and the diameter should be accessible from the Circle instance. It should not be allowed to manipulate the diameter or the area from outside of the class. For performance reasons, the area and diameter should only be calculated once.

So the class would look like this:

So all of the requirements are fulfilled. However, in Swift there is a very good way to make this even shorter:

Properties With Private Setters

By using  private(set) in front of a property, a property has the default access level for the getter, but a private setter. So we can drop the two getters:

Of course it is also possible to have public getters for the properties:


In this example the properties were just double values. However, if it is an object, it could be manipulated by using one of the object’s methods! The use of a private setter would just permit to assign a complete new object. Keep that in mind, when you are using private setters.


Image: @ 1000 Words / shutterstock.com
Swift: Access Control