본문 바로가기
OLD_달려라/Android

Kotiln Error ] 'break' and 'continue' are not allowed in 'when' statements. Consider using labels to continue/break from the outer loop

by 달승 2021. 1. 15.

아래 코드를

while (...) {
    when (itemType) {
        1 -> continue
        else -> break
    }
}

 

이렇게 수정해줍니다.

loop@ while (...) {
    when (itemType) {
        1 -> continue@loop
        else -> break@loop
    }
}

 

 

@label을 사용하는 이유

Any expression in Kotlin may be marked with a label.
Labels have the form of an identifier followed by the @ sign, for example: abc@, fooBar@ are valid labels(...) A break qualified with a label jumps to the execution point right after the loop marked with that label. A continue proceeds to the next iteration of that loop.
 

How to `continue` or `break` in a `when` statement inside a `while` loop using Kotlin

I am converting a large project to Kotlin. There have been numerous challenges. Me learning the new patterns of Kotlin is one of them. Hopefully there is a pattern I can use to resolve this one. ...

stackoverflow.com

 

댓글