在Kotlin有一个相当于Swift代码下面?

if let a = b.val {

} else {

}

当前回答

If let语句

Swift if let可选绑定(称为if-let语句)用于提取存在的非可选值,或者如果值为nil则不执行任何操作。

斯威夫特的if-let声明:

let b: Int? = 50

if let a: Int = b {
    print("Good news!")
} else {
    print("Equal to 'nil' or not set")
}

/*  RESULT: Good news!  */

在Kotlin中,就像在Swift中一样,为了避免在不期望的情况下试图访问空值而导致的崩溃,提供了一个特定的语法(如第二个例子中的b.let{})来正确地展开可空类型:

Kotlin相当于Swift的if-let语句:

val b: Int? = null
val a = b

if (a != null) { 
    println("Good news!")
} else { 
    println("Equal to 'null' or not set")
}

/*  RESULT: Equal to 'null' or not set  */

Kotlin的let方法与安全调用操作符?:结合使用时,提供了一种处理可空表达式的简洁方法。

Kotlin的内联let函数和Swift的nil合并运算符的Elvis运算符:

val b: Int? = null

val a = b.let { nonNullable -> nonNullable } ?: "Equal to 'null' or not set"
println(a)

/*  RESULT: Equal to 'null' or not set  */

警卫让声明

Swift中的guard-let语句简单而强大。它检查某些条件,如果计算结果为false,则执行else语句,该语句通常会退出一个方法。

让我们来看看斯威夫特的这句话:

let b: Int? = nil

func method() {
    guard let a: Int = b else {
        print("Equal to 'nil' or not set")
        return
    }
    print("Good news!")
}
method()

/*  RESULT: Equal to 'nil' or not set  */

Kotlin对霉霉的保守声明也有类似的影响:

与Swift不同的是,在Kotlin中根本没有警卫声明。但是,您可以使用Elvis Operator - ?:来获得类似的效果。

val b: Int? = 50

fun method() {
    val a = b ?: return println("Equal to 'null' or not set")
    return println("Good news!")
}
method()

/*  RESULT: Good news!  */

其他回答

If let语句

Swift if let可选绑定(称为if-let语句)用于提取存在的非可选值,或者如果值为nil则不执行任何操作。

斯威夫特的if-let声明:

let b: Int? = 50

if let a: Int = b {
    print("Good news!")
} else {
    print("Equal to 'nil' or not set")
}

/*  RESULT: Good news!  */

在Kotlin中,就像在Swift中一样,为了避免在不期望的情况下试图访问空值而导致的崩溃,提供了一个特定的语法(如第二个例子中的b.let{})来正确地展开可空类型:

Kotlin相当于Swift的if-let语句:

val b: Int? = null
val a = b

if (a != null) { 
    println("Good news!")
} else { 
    println("Equal to 'null' or not set")
}

/*  RESULT: Equal to 'null' or not set  */

Kotlin的let方法与安全调用操作符?:结合使用时,提供了一种处理可空表达式的简洁方法。

Kotlin的内联let函数和Swift的nil合并运算符的Elvis运算符:

val b: Int? = null

val a = b.let { nonNullable -> nonNullable } ?: "Equal to 'null' or not set"
println(a)

/*  RESULT: Equal to 'null' or not set  */

警卫让声明

Swift中的guard-let语句简单而强大。它检查某些条件,如果计算结果为false,则执行else语句,该语句通常会退出一个方法。

让我们来看看斯威夫特的这句话:

let b: Int? = nil

func method() {
    guard let a: Int = b else {
        print("Equal to 'nil' or not set")
        return
    }
    print("Good news!")
}
method()

/*  RESULT: Equal to 'nil' or not set  */

Kotlin对霉霉的保守声明也有类似的影响:

与Swift不同的是,在Kotlin中根本没有警卫声明。但是,您可以使用Elvis Operator - ?:来获得类似的效果。

val b: Int? = 50

fun method() {
    val a = b ?: return println("Equal to 'null' or not set")
    return println("Good news!")
}
method()

/*  RESULT: Good news!  */

Swift如果让声明在Kotlin

简短的回答是使用简单的IF-ELSE,因为在这篇评论的时候,在Kotlin LET中没有等效的东西,

    if(A.isNull()){
// A is null
    }else{
// A is not null
    }

这是我的变体,仅限于非常常见的“if not null”情况。

首先,在某个地方定义它:

inline fun <T> ifNotNull(obj: T?, block: (T) -> Unit) {
    if (obj != null) {
        block(obj)
    }
}

它可能应该是内部的,以避免冲突。

现在,转换这个Swift代码:

if let item = obj.item {
    doSomething(item)
}

到这个Kotlin代码:

ifNotNull(obj.item) { item -> 
    doSomething(item)
}

请注意,在Kotlin中,你可以删除参数并使用它:

ifNotNull(obj.item) {
    doSomething(it)
}

但是如果代码块超过1-2行,最好是显式的。

这是我所能找到的最相似的斯威夫特。

我添加这个答案是为了澄清已接受的答案,因为它太大了,不适合评论。

这里的一般模式是,你可以使用Kotlin中可用的范围函数的任何组合,由Elvis操作符分隔,如下所示:

<nullable>?.<scope function> {
    // code if not null
} :? <scope function> {
    // code if null
}

例如:

val gradedStudent = student?.apply {
    grade = newGrade
} :? with(newGrade) {
    Student().apply { grade = newGrade }
}

如果b是一个成员变量,那么这种方法对我来说似乎是最有可读性的:

val b = this.b
if (b == null) {
    return
}
println("non nullable : ${b}")

这也与它在swift中的工作方式一致,在swift中,一个新的局部变量遮蔽了成员变量。