Skip to Content

Reactive Swift: Access Control for Mutable Properties

Swift’s access control allows us to define separate access levels for the getter and setter of a property. This is nice if we want to provide read access to a property but should only be able to modify it internally:

private(set) var isLoading = false

When using ReactiveSwift (or similarly RxSwift) you usually work with MutableProperty. In order to limit modification of the property’s value we can create a private writable MutableProperty and a public read-only Property:

private let isLoadingMutable = MutableProperty(false)
lazy var isLoading: Property<Bool> = Property(self.isLoadingMutable)

The read-only Property is now dependent on the MutableProperty and will always reflect it’s value.

Looking for a tool to manage all your software translations in one single place?
Checkout the new localization and translation management platform.