swift - Kotlin 属性初始化(就像 swift 使用闭包)

标签 swift kotlin

在想将我的移动开发从 iOS 扩展到 Android 之后,我最近开始研究 Kotlin,因为我试图找到类似于 swift 允许您使用闭包初始化属性的方式。

//Swift init using closure
let myLabel: UILabel = {
    let label = UILabel()
    // Init Code

    return label
}()

到目前为止,我还没有能够通过一些谷歌搜索找到它,并且想知道这是否可能。这可能吗?或者甚至有一些解决方法?

最佳答案

您可以使用 apply() :

val myLabel : UILabel().apply {
    // in this block, `this` is the UILabel being initialized 
    color = "blue"
    text = "hello"
}

关于swift - Kotlin 属性初始化(就像 swift 使用闭包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57528476/

相关文章:

ios - 如何判断自定义 UISearchBar 中的 UITableView 是否被触摸?

gradle - 使用 gradle kotlin dsl 执行 JavaExec 任务

android - kotlin 中的 MPAndroid 饼图

ios - Firestore在文档中的数组中搜索字符串

ios - 核心数据搜索和更新

android - 使用新数据调用invalidate()后,MP Android Chart消失

android - 如何替换 Android 中不推荐使用的 Bundle/Argument get(key) 调用

android - 显示或隐藏AppBar和Toolbar-取决于 fragment

swift - 带有 Firebase 的 iOS 应用程序不会以缓慢的互联网连接启动

ios - 如何上传录制的音频文件进行解析?