I defined enums as confirming to a protocol Eventable:
protocol Eventable {
var name: String { get }
static var all: [Eventable] { get }
}
enum MyEnum: String, Eventable {
case bla = "bla"
case blu = "blu"
var name: String {
return self.rawValue
}
static var all: [Eventable] {
return [
MyEnum.bla,
MyEnum.blu
]
}
}
I have other enums like MyEnum also under the form: enum Bla: String, Eventable { }
I have two questions:
for the enums having a String data type, I would like to avoid duplicating the generation of the variable name:
var name: String
I am not sure how to write that in Swift. I tried to play around with the "where" clause but not success. How can I achieve that?when I write my enums and conform to the protocol for that part:
static var all: [Eventable] { get }
. I would like that for the enum MyEnum, it constrains the variable to:static var all: [MyEnum] { ... }
because for now I can put in the returned array any element being an Eventable and it's not what I need. Amongst other things, I tried to define a generic constraint in the protocol for it, but I get the following error:
Protocol 'Eventable' can only be used as a generic constraint because it has Self or associated type requirements
Thank you very much for the help!