在Kotlin有一个相当于Swift代码下面?
if let a = b.val {
} else {
}
在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! */
其他回答
如果让我们使用内联的乐趣,我们可以得到像Swift一样的unwring语法
inline fun <T:Any?> T?.unwrap(callback: (T)-> Unit) : Boolean {
return if (this != null) {
this?.let(callback)
true
}else {
false
}
}
用途::
val name : String? = null
val rollNo : String? = ""
var namesList: ArrayList<String>? = null
if (name.unwrap { name ->
Log.i("Dhiru", "Name have value on it $name")
})else if ( rollNo.unwrap {
Log.i("Dhiru","Roll have value on it")
}) else if (namesList.unwrap { namesList ->
Log.i("Dhiru","This is Called when names list have value ")
}) {
Log.i("Dhiru","No Field have value on it ")
}
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 a = b.val {
//use "a" as unwrapped
}
else {
}
在芬兰湾的科特林:
b.val?.let{a ->
//use "a" as unwrapped
} ?: run{
//else case
}
Any的问题是什么?让{}?:运行{}结构是:
它只允许每条语句进行一次非空检查 如果let块返回null,运行块无论如何都会被求值 不可能在switch/when样式中执行多次检查
解决这些问题的一个解决方案是定义如下的函数:
private inline fun <A> ifNotNull(p1: A?, block: (A) -> Unit): Unit? {
if (p1 != null) {
return block.invoke(p1)
}
return null
}
private inline fun <A, B> ifNotNull(p1: A?, p2: B?, block: (A, B) -> Unit): Unit? {
if (p1 != null && p2 != null) {
return block.invoke(p1, p2)
}
return null
}
private inline fun <A, B, C> ifNotNull(p1: A?, p2: B?, p3: C?, block: (A, B, C) -> Unit): Unit? {
if (p1 != null && p2 != null && p3 != null) {
return block.invoke(p1, p2, p3)
}
return null
}
这将允许这样的语句:
ifNotNull(a, b) { a, b ->
// code when a, b are not null
} ?:
ifNotNull(c) { c ->
// code when a, b are null and c not null
} ?:
ifNotNull(d, e, f) { d, e, f ->
// code when a, b, c are null and d, e, f not null
} ?: run {
// code which should be performed if a, b, c, d, e and f are null
}
唯一需要注意的是,与Swift的if let相比,如果在循环中执行,则不支持continue和break语句。
在kotlin中也有类似的方法来实现Swift的if-let风格
if (val a = b) {
a.doFirst()
a.doSecond()
}
您还可以分配多个可空值
if (val name = nullableName, val age = nullableAge) {
doSomething(name, age)
}
如果可空值被使用超过1次,这种方法将更适合。在我看来,它从性能方面有所帮助,因为可为空的值将只检查一次。
来源:Kotlin Discussion