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.

Encapsulation

Encapsulation means basically that information and states of a class should be hidden from outside of a class, if only the class itself should manipulate it. And 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.

Example

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’s 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 a 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:

Objects

In this example the properties were just double values. However, if it is a 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.

References

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

Book Tip

Big Nerd Range Guide: iOS Programming: Excellent introduction to iOS development. Some programming experience is recommended.

Amazon