A few days ago, I have asked this question.
The brilliant solution was like this:
enum MyEnum {
case one
case two
case three(user: String)
func isOfSameType(_ other: MyEnum) -> Bool {
switch (self, other) {
case (.one, .one):
return true
case (.two, .two):
return true
case (.three, .three):
return true
default:
return false
}
}
}
var array: [MyEnum] = []
func add(_ value: MyEnum) {
if array.contains(where: { value.isOfSameType($0) }) { return }
array.append(value)
}
Now, analyzing that with cold blood, this seems to be completely black magic.
How can the switch compare things without parameters?
But now I need to check if the last element on stack
is of kind three
.
Something like
if array!.last.isOfType(.three) {
}
This will not work. Xcode will require paremeters from .three.
Because I am not understanding what I have so far, I cannot figure out how to do that.