S. Birklin S. Birklin - 7 months ago 12
Swift Question

Swift Initialization of constant variables in protocol

In Java you can initialize final static Strings in an interface. Is there also a method in Swift?

Answer

No swift doesn't support that. My advice is to define a struct alongside your protocol and define all constants as immutable static stored properties. For example:

protocol MyProtocol {
}

struct MyProtocolConstants {
    static let myConstant = 10
}

Note that structs are preferred to classes, for at least one reason: classes don't support static stored properties (yet)

Comments