-4

MutableList use as keyword

`when(msg?.what){
    MESSAGE_GET_LIST -> {
     if (msg.obj != null && msg.obj is MutableList<*>){
                        list = msg.obj as MutableList<BaseIncomeBean>
                        ` 

when I received a message and want to use it ,I must covert to MutableList use as keyword

Finally build my project and appear following warning

Warning:(51, 40) Unchecked cast: Any! to MutableList<BaseIncomeBean>

How do I fix this warning?

user6639068
  • 43
  • 1
  • 5

2 Answers2

0

Can you change if statement :

if(msg.arg1 == AsyncActionCallback.RESULT_OK && msg.obj != null && msg.obj is MutableList<BaseIncomeBean>)

from MutableList<*> to MutableList

"*" mean any in kotlin, Object in java. If you return an object reference, you must check whether the returned value is the class reference before casting another class. Because the return value is marked as an object, a different class reference may be returning. The IDE does not know that.

DummytoDummy
  • 553
  • 1
  • 5
  • 13
0

Kotlin has smart casts. But Generics are erased at runtime. Thus you never know the type of list contents. It would work to just cast it to MutableList<*>:

if (msg.obj is MutableList<*>)
    list = msg.obj as MutableList<*>

But since you set up your project, you know, that the cast should be successful. In this case you could just tell the compiler to ignore the warning:

if (msg.obj is MutableList<*>)
    @Suppress("UNCHECKED_CAST")
    list = msg.obj as MutableList<BaseIncomeBean>
tynn
  • 38,113
  • 8
  • 108
  • 143