I have an enum like this:
enum Rank: CaseIterable {
case Ace, King, Queen, ...
}
and want to make it conform to Stridable. I tried to implement the func distance(to other: _) by getting the index of the card like so:
func distance(to other: Rank) -> Int {
let indexOfSelf = Rank.allCases.firstIndex(of: self)!
let indexOfOther = Rank.allCases.firstIndex(of: other)!
return indexOfOther - indexOfSelf
}
This works fine and as intended as long as I don't conform to Stridable, e.g. ace.distanceTo(Queen) results in 2. However, as soon as I conform to Stridable, the code breaks into an infinite loop and I get the error EXC_BAD_ACCESS (code=2, address=x).
Is that supposed to happen? And if so, why is that happening?
Thanks for all help!
My implementation of advanced(by n: Int):
func advanced(by n: Int) -> Rank {
let index = Rank.allCases.firstIndex(of: self)!
let resultIndex = index + n
if resultIndex > Rank.allCases.count {
return .Two
}
return Rank.allCases[resultIndex]
}
something that would cause the error:
call of: ace.distanceTo(Queen) would break into an infinite loop