Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

346
Visualizações
Swift: Cannot override mutable property with read-only property 'xxx'

I don't understand the meaning of this error. This is what I am trying to do:

protocol P {
    var testTitle: String? { get }
}

class MyViewController: UIViewController, P {
    var testTitle: String? {
        didSet {
            self.title = testTitle
        }
    }
}

final class TestViewController: MyViewController {
    var testTitle: String? { // Error occurs here
        return "Test" 
    }
}

And I'm getting:

Cannot override mutable property with read-only property 'testTitle'

Could you help me understanding what's going on? Maybe it's trivial for some people but not for me.

Thanks for your help.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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
        }
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda