Sad*_*ana 3 kotlin kotlin-android-extensions kotlinx.coroutines
fun onYesClicked(view: View) {
launch(UI) {
val res = post(context!!,"deleteRepo")
if(res!=null){
fetchCatalog(context!!)
catalogActivityCatalog?.refresh()
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常。我想通过返回(从而停止执行)if来避免if中的嵌套部分res == null,像这样,
fun onYesClicked(view: View) {
launch(UI) {
val res = post(context!!,"deleteRepo")
if(res==null)return //this line changed <---A
fetchCatalog(context!!) //moved outside if block
catalogActivityCatalog?.refresh() //moved outside if block
}
}
Run Code Online (Sandbox Code Playgroud)
它说,当我在<-A所示的行中使用return时,此处不允许使用“ return”
这里有关键字可以退出launch屏蔽吗?在这里可以使用替代而不是返回的替代方法是什么?
必须使用以下命令指定退货目的地 return@...
fun onYesClicked(view: View) {
launch(UI) {
val res = post(context!!,"deleteRepo")
if(res==null)return@launch //return at launch
fetchCatalog(context!!)
catalogActivityCatalog?.refresh()
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您必须创建一个应用 return 语句的标签,例如return@label:
fun onYesClicked(view: View) {
label@launch(UI) {
val res = post(context!!,"deleteRepo")
if(res==null) return@label //this line changed <---A
fetchCatalog(context!!) //moved outside if block
catalogActivityCatalog?.refresh() //moved outside if block
}
}
Run Code Online (Sandbox Code Playgroud)
这是从 lambda 返回的 Kotlin 方式。
| 归档时间: |
|
| 查看次数: |
1169 次 |
| 最近记录: |