S. Birklin S. Birklin - 2 years ago 120
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 Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download