标签并不表示forEach中的循环

我从Kotlin的一个循环中继续,但是我从工作室得到警告,标签不表示循环。 有人能告诉我什么是错的语法?

这是代码段

newRooms.forEach roomloop@ { wallRoom: WallRoom -> val index = rooms.indexOf(wallRoom) if(index!=-1) { val room = rooms[index] //get the corresponding room. //check if the last session is same in the room. if(wallRoom.topics.last().fetchSessions().last()==room.topics.last().fetchSessions().last()) { continue@roomloop } 

这里标记的lambda表达式是一个函数文字,而不是一个循环。

您不能在这里breakcontinue lambda表达式,因为它独立于for循环。

 public inline fun <T> Array<out T>.forEach(action: (T) -> Unit): Unit { for (element in this) action(element) } 

您可以使用return从函数返回。

 return@roomloop 

请注意,下面的代码段与另一个代码段的行为相同,它们都将打印123

 arrayOf(1, 2, 3).forEach label@ { print(it) return@label } label@ for (i in arrayOf(1, 2, 3)) { print(i) continue@label }