๐Ÿ“˜break

๐Ÿšง ๆ–ฝๅทฅไธญ

Swift โŸฉ flow control โŸฉ control transfer โŸฉ break

โ›” error: unlabeled 'break' is only allowed inside a loop or switch, a labeled break is required to exit an if or do. ( ๐Ÿ‘‰ see: for...in vs. forEach )

(1...10).forEach { n in 
    
    if n > 2 { break }    // โญ `break` is inside a "closure"
    //         ^^^^^
    // โ›” error: 
    //  โ€ข unlabeled 'break' is only allowed inside a loop or switch
    //  โ€ข labeled 'break' is required to exit an `if` or `do`
}

Last updated