gpt4 book ai didi

android - 该标签不表示 forEach 中的循环

转载 作者:行者123 更新时间:2023-11-29 15:38:17 25 4
gpt4 key购买 nike

我从 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
}

关于android - 该标签不表示 forEach 中的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728687/

25 4 0