如何在 kotlin 中为函数参数设置 not null

N S*_*rma 2 android kotlin

我正在创建一个函数Kotlin。它验证电子邮件和密码字段。我想申请电子邮件,密码不能为空。@NotNull这里有点注释。

有谁知道如何做到这一点Kotlin?因此调用者无法发送空值。

private fun isEmailAndPasswordValid(email: String, password: String): Boolean {

    if (email.isEmpty()) return false

    if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) return false

    if (password.isEmpty()) return false

    return true
}
Run Code Online (Sandbox Code Playgroud)

s1m*_*nw1 5

Kotlin 通过nullablenot-nullable来区分所有类型。例如,类String可用于String不可为空的 type ,以及String?可空的 type ,即可以保存null

\n\n

在您的示例中,没有使用可空类型,因此您\xe2\x80\x99都很好 - 不需要额外的注释。

\n\n

应研究该文档以获取更多信息。

\n