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

371
Visualizações
Swift Protocol extension: cannot assign to property: '' is a get-only property

I think I don't understand how protocol extensions are supposed to work.

I tried this:

protocol P {
    var h: [String: Any] { set get }
}

extension P {
    var h: [String: Any] {
        get {
            return [:]
        }
        set {}
    }
}

struct S: P {   
    init() {
        self.h = ["o": "o"]
    }
}

My goal is that S has the properties of P and doesn't need to re-declare it in the struct definition.

However, when I create let s = S(), s.h always equals [:] and not ["o":"o"].

Of course, this is because my setter is empty, but I don't know how to do what I want to achieve here.

Thanks for your help.

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

0

My goal is that S has the properties of P and doesn't need to re-declare it in the struct definition.

That's not possible in the way you're trying to do this. Protocols require certain behaviors. Conforming types must provide those behaviors. If you require storage to implement the behavior, than the conforming type has to provide the storage.

It's fine if you don't want storage, then the extension can just return values like yours does. But you can't return computed values and also have storage. What you're trying to do isn't possible. You're thinking of classes, not protocols. (If you want classes, that's fine. Use classes. There's nothing wrong with them.)

A good way to think about why this isn't possible in Swift is to consider the following case. Assume your P protocol exists in some form that achieves what you want. Now in one module we define:

struct S {}

This struct requires no storage. Any calls to S() in that module allocate nothing. (Alternately, give S some properties, and it'll allocate some specific amount of memory.)

Now in some other module, extend S to conform to P:

extension S: P {}

Where would the storage for h go? Instances may already exist at the point that this extension is even loaded. What if there were multiple protocols that wanted additional storage and all were attached to S. What offsets into the S struct should those properties be? The order would be very important to compiled code. This isn't impossible to solve (ObjC does it using a technique called associated objects), but it's a large addition to the language, and prevents a number of important optimizations, and Swift doesn't do things that way.

over 4 years ago · Santiago Trujillo Relatório

0

You were close, you just have to restate the h variable in the S Struct

protocol P {
    var h: [String: Any] { set get }
}

extension P {
    var h: [String: Any] {
        get {
            return [:]
        }
        set {}
    }
}

struct S: P {
    var h: [String: Any]

    init() {
        self.h = ["o": "o"]
    }
}

let s = S()
print(s.h) // ["o": "o"]
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