我想遍历枚举中的所有值,但考虑到某些条件,我想放弃并转到下一个值.任何在breakcontinue语句中的try ,如果编译器指出‘.allCases.forEach’不是‘func Test1’中的循环.

我想出的最好的解决办法是func test2个,但它似乎"不快",我可以只有一堆嵌套的if个块,看起来像是额外的.那么,如果不是循环,.allCases.forEach又是什么呢?更重要的是,有没有一种不同/更好的方法来迭代enum

更新:添加了Func‘process FoolPower’,解决方案1作为目标的一个小示例.想想大棋盘吧.

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)
}


    mutating func processFoolPower(foolPiece: gamePiece, owner: Bool) {

        var testPiece: gamePiece = MockData.blankPiece
        
        for direction in moveDirections.allCases {
            let testSquare = foolPiece.location! + SquareShift(direction)
            if testSquare < 0 || testSquare > 143  { continue }                                     // test for off top/bottom of board
            switch direction {
            case .down, .up, .left, .right:
                if isStraight(from: foolPiece.location!, to: testSquare) == .invalid { continue }   // test for off sides of board
            case .upLeft, .downLeft, .upRight, .downRight: ()
                if isDiagonal(from: foolPiece.location!, to: testSquare) == .invalid { continue }   // test for off sides of board
            case .invalid: ()
            }
            testPiece = pieces.first(where: { $0.location == testSquare }) ?? MockData.blankPiece
            if testPiece.status != .dead { executeKill(pieceId: testPiece.id, explanation: .blownUp) }
        }
        executeKill(pieceId: foolPiece.id, explanation: .exploded)
    }

推荐答案

forEach是一个迭代元素的数组函数,调用闭包,就像map;它不是SWIFT循环构造,所以不能使用breakcontinue.

你的第二次try 更接近你所需要的,但不必要地复杂.不需要索引或原始值.

您可以直接对集合的元素使用for in循环:

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)
}

Swift相关问答推荐

如何在Swift 中创建移动的uil标签?

防止NavigationLink自行返回导航视图

.onReceive NSWindow.CloseNotify是否会为App中的每个窗口调用?

在SwiftUI中使用自定义图像填充列表行的正确方法

无法创建MKAssociateRegion对象

在不传递参数的情况下使用Init方法?

如何在SwiftUI中做出适当的曲线?

在 Swift 的异步上下文中,如何指示我想要函数的同步版本?

如何在类中打印函数

我如何遵守与演员的协议?

您可以在运行时访问 Picker 元素并更改它们的 colored颜色 吗

如何让动画变慢 SwiftUI

SwiftUI:调用以编程方式创建的视图的函数

Vapor - 流利的,将对象保存到 PostgreSQL

NSFontAttributeName 已更改为 String

Swift 2 中的新 @convention(c):我该如何使用它?

iOS:检测设备是否为 iPhone X 系列(无框)

如何从一个可观察的数组创建一个可观察的数组?

不能从非开放类继承 swift

通过单击表格视图中的单元格打开新的视图控制器 - Swift iOS