在Kotlin中,处理可空值、引用或转换这些值的惯用方法是什么?
Xyz?
Xyz
val something: Xyz? = createPossiblyNullXyz()something.foo() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Xyz?"
val something: Xyz? = createPossiblyNullXyz()if (something != null) { something.foo() }
null
if
null
get()
null
val map = mapOf("a" to 65,"b" to 66,"c" to 67)val something = map.get("a")something.toLong() // Error: "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Int?"
get()
Int?
达令说