๐Ÿ…ฟ๏ธCaseIterable+ext

๐ŸŒ€Extensions

case.next()

extension CaseIterable where Self: Equatable {
    /// next case (circular)
    public func next() -> Self {
        let cases = Self.allCases
        let i = cases.firstIndex(of: self)!
        var j = cases.index(after: i)
        if j == cases.endIndex { j = cases.startIndex }
        return cases[j]
    }
}

๐Ÿ”— Learn SwiftUI using Swift Playgrounds

CaseIterable.randomCase

extension CaseIterable {
	static var randomCase: AllCases.Element {
		return allCases.randomElement()!
	}
}

๐Ÿ”— Enumerating enum cases in Swift

๐Ÿ“˜ๅƒ่€ƒ่ณ‡ๆ–™

Last updated