yattee/Extensions/CaseIterable+Next.swift

16 lines
411 B
Swift
Raw Normal View History

extension CaseIterable where Self: Equatable {
2021-07-07 22:39:18 +00:00
func next(nilAtEnd: Bool = false) -> Self! {
let all = Self.allCases
2021-06-26 09:39:35 +00:00
let index = all.firstIndex(of: self)!
let next = all.index(after: index)
2021-07-07 22:39:18 +00:00
if nilAtEnd == true {
if next == all.endIndex {
return nil
}
}
return all[next == all.endIndex ? all.startIndex : next]
}
}