Swift: Cannot override mutable property with read-only property 'xxx'

The protocol requires that the adopting class implements a property testTitle with a getter but it does not forbid to declare the property with getter and setter.

To override a property you have to override the (entire) signature in the class, not the protocol requirement.

As testTitle is declared as read/write you cannot override it as read-only.


You cannot implement a subclass to be more restrictive than the base class (it would only work the other way round). So if the base class implements a read/write property, you cannot make it read only in your derived class.

Therefore:

final class TestViewController: MyViewController {
    override var testTitle: String? { // No error occurs here 
        get {
            return "Test" 
        }
        set {
            super.testTitle = newValue
        }
    }
}

Tags:

Swift