aMethodThatBelongsToMyType
全部标签 下面的代码将无法编译,因为变量myType可以为null。有没有办法在Kotlin中为可空类型执行withblock?valmyType:MyType?=nullwith(myType){aMethodThatBelongsToMyType()anotherMemberMethod()} 最佳答案 您可以使用后缀!!将可空类型转换为不可空类型:with(myType!!){aMethodThatBelongsToMyType()anotherMemberMethod()}如果该值确实为null,它会抛出一个NullPointerEx
下面的代码将无法编译,因为变量myType可以为null。有没有办法在Kotlin中为可空类型执行withblock?valmyType:MyType?=nullwith(myType){aMethodThatBelongsToMyType()anotherMemberMethod()} 最佳答案 您可以使用后缀!!将可空类型转换为不可空类型:with(myType!!){aMethodThatBelongsToMyType()anotherMemberMethod()}如果该值确实为null,它会抛出一个NullPointerEx