yattee/Extensions/CaseIterable+Next.swift
2021-07-08 00:54:35 +02:00

16 lines
411 B
Swift

extension CaseIterable where Self: Equatable {
func next(nilAtEnd: Bool = false) -> Self! {
let all = Self.allCases
let index = all.firstIndex(of: self)!
let next = all.index(after: index)
if nilAtEnd == true {
if next == all.endIndex {
return nil
}
}
return all[next == all.endIndex ? all.startIndex : next]
}
}