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

260
Visualizações
Need to satisfy Swift protocol requirement by using a specific subclass of the requirement (or type that conforms to it)

I have a protocol that I've created (in Swift 4.2), and one of its requirements is a property that is of the same type as the protocol itself.

As an example, I have a protocol defined like so:

protocol A {
    var a: A? { get set }
}

I have several Models that conform to this protocol:

class Model1: A {
    var a: A?
}
class Model2: A {
    var a: A?
}

For one of my models, I need to satisfy the protocol requirement by being more specific for the property defined by variable a (i.e. the variable with the protocol type). So for example I may want to implement Model2 as:

class Model2: A {
    var a: Model1?
}

In this case since Model1 conforms to the protocol A you would expect this to be able to satisfy the protocol requirement, however I get an error instead:

Type 'Model2' does not conform to protocol 'A'

Why is this happening, and what can I do to make it work as described above?

Appendix

I've modelled the above scenario in an Xcode Playground and here is a screenshot of the error I'm seeing. enter image description here

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

0

To conform to protocol A, Model2 would need a member var a that allows storing a reference to anything conforming to protocol A, not just a reference to a Model1. So you can't do this.

over 4 years ago · Santiago Trujillo Relatório

0

You could do this with associated types:

protocol A {
    associatedtype B: A
    var a: B? { get }
}

This would let you declare Model2 with the specificity you wish:

class Model2: A {
    var a: Model1?
}

But unfortunately, that would mean you could no longer declare variables of type A. To fix that, you could use generic models:

class Model1<T: A>: A {
    var a: T?
}
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