Sobes.tech
Junior — Middle

Is it possible to implement variables within protocols?

sobes.tech AI

Answer from AI

In Swift, protocols can contain property declarations but cannot store variables with state. That is, within a protocol, you can define properties with get and set, which must be implemented in classes or structures, but the protocol itself does not store data. For example:

protocol MyProtocol {
    var name: String { get set }
}

struct User: MyProtocol {
    var name: String
}

Thus, the implementation of variables (storing state) occurs in the types that conform to the protocol, not in the protocol itself.

Is it possible to implement variables within… - sobes.tech