Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why on enums is .allCases.forEach not a loop (continue and break don't work)?

I want to iterate through all values in an enum but given certain conditions I want to give up and move to the next value. Any attempt at a break or continue statement and the compiler says that:

.allCases.forEach is not a loop as in func test1.

The best workaround I’ve come up with is func test2 but it seems "non-swifty" and I could just have a bunch of nested if blocks that seem extra. So what is .allCases.forEach if not a loop and more importantly, is there a different/better way to iterate through the enum?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enum directions: Int, CaseIterable {
    case up = 0, upRight, right, downRight, down, downLeft, left, upLeft, invalid
}

func test1() {
    print("forEach: ")
    moveDirections.allCases.forEach {
        if $0 == .up { () }         // I want to { continue } here to next direction
        if $0.rawValue == 4 { () }  // or { continue } here to next direction
        testPrint(test: $0)
    }
}

func test2() {
    print("for : ")
    for direction in 0..<moveDirections.allCases.count {
        if moveDirections(rawValue: direction) == .up { continue }
        if direction == 4 { continue }
        testPrint(test: moveDirections(rawValue: direction).self!)
    }
}

func testPrint(test: moveDirections) {
    print(test.self)
}

>Solution :

forEach is an array function that iterates over the elements, invoking a closure, like map; It isn’t a Swift loop construct, so you can’t use break or continue.

Your second attempt is closer to what you need, but unnecessarily complicated. There is no need for an index or raw values.

You can use a for in loop with a collection’s elements directly:

enum directions: Int, CaseIterable {
    case up = 0, upRight, right, downRight, down, downLeft, left, upLeft, invalid
}

for direction in directions.allCases { 
    if direction == .up { 
        continue 
    }
    print(direction)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading