我确定,我的代码返回的值不是空值:
fun getBitmap(id: Int): Bitmap{
if (!(id in bitmapStorage))
bitmapStorage.put(id, BitmapFactory.decodeResource(resources, id))
return bitmapStorage.get(id)
}
返回Bitmap
类型而不是Bitmap?
的正确方法是什么?
[!!
,as Bitmap
或其他?
您可以使用!!
运算符或MutableMap的API-getOrPut
,它将返回非null类型
fun getBitmap(id: Int): Bitmap{
return bitmapStorage.getOrPut(id) {BitmapFactory.decodeResource(resources, id)}
}
请注意,如果同时修改地图,则不能保证该操作是原子操作。