我从 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
}
Run Code Online (Sandbox Code Playgroud)
这里标记的 lambda 表达式是一个函数字面量,而不是一个循环。
你不能break或continuelambda表达式在这里,因为它是独立于for循环。
public inline fun <T> Array<out T>.forEach(action: (T) -> Unit): Unit {
for (element in this) action(element)
}
Run Code Online (Sandbox Code Playgroud)
您可以使用return从函数返回。
return@roomloop
Run Code Online (Sandbox Code Playgroud)
请注意,下面的代码段的行为与另一个相同,它们都将打印123:
arrayOf(1, 2, 3).forEach label@ {
print(it)
return@label
}
label@ for (i in arrayOf(1, 2, 3)) {
print(i)
continue@label
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1885 次 |
| 最近记录: |