android - Kotlin 从嵌套的 forEach 返回@forEach
问题描述
我想break
在嵌套的 forEach 循环中进行类似的操作,以过滤我的 searchView 中的数据(如果数据内容在我的搜索中包含任何单词)。
val filtered = mutableListOf<EventEntity>()
rawDataList.forEach {data ->
text.split(' ').forEach { word ->
if (data.content.contains(word, ignoreCase = true)) {
filtered.add(data)
return@forEach // **There is more than one label with such a name in this scope**
}
}
}
我的情况是否存在优雅的解决方案?
解决方案
如果您遇到此错误并且无法使用内置函数修复它,您可以通过name@
在块之前添加将自定义标签应用于 lambda:
rawDataList.forEach outer@{data ->
text.split(' ').forEach { word ->
if (data.content.contains(word, ignoreCase = true)) {
filtered.add(data)
return@outer
}
}
}
推荐阅读
- subdomain - 在 Marketing Cloud 中设置与 SAP 相同的子域,同时将其用于不同的营销平台是否存在问题?
- ansible - 主机上的任务调用之间的 Ansible 暂停
- palantir-foundry - 使用 API 访问 Palantir Foundry Fusion
- java - 如何在 Hazelcast Imap 过滤器期间控制处理器数量
- javascript - 如何使用Javascript无限滚动水平文本?
- python - 评论表单显示不正确
- sharepoint - 切换相关复选框时覆盖字段
- html - 如何对结果进行连续排序?
- ios - 向左/向右滑动时 UITableView 选择消失
- r - 无法取消嵌套 json 文件以在 r 中创建地图?