safeUnbox() cannot be inverted

前端 未结 4 855
孤独总比滥情好
孤独总比滥情好 2021-01-06 05:15

I\'m trying to eliminate all the warnings of my Android application and one of them is this:

viewModel.value is a boxed field but needs to be un-boxed

4条回答
  •  一向
    一向 (楼主)
    2021-01-06 06:11

    I find a code ,this this safeUnbox implementation。

    /** @hide */
    protected static int safeUnbox(java.lang.Integer boxed) {
        return boxed == null ? 0 : (int)boxed;
    }
    
    /** @hide */
    protected static long safeUnbox(java.lang.Long boxed) {
        return boxed == null ? 0L : (long)boxed;
    }
    
    /** @hide */
    protected static short safeUnbox(java.lang.Short boxed) {
        return boxed == null ? 0 : (short)boxed;
    }
    
    /** @hide */
    protected static byte safeUnbox(java.lang.Byte boxed) {
        return boxed == null ? 0 : (byte)boxed;
    }
    
    /** @hide */
    protected static char safeUnbox(java.lang.Character boxed) {
        return boxed == null ? '\u0000' : (char)boxed;
    }
    
    /** @hide */
    protected static double safeUnbox(java.lang.Double boxed) {
        return boxed == null ? 0.0 : (double)boxed;
    }
    
    /** @hide */
    protected static float safeUnbox(java.lang.Float boxed) {
        return boxed == null ? 0f : (float)boxed;
    }
    
    /** @hide */
    protected static boolean safeUnbox(java.lang.Boolean boxed) {
        return boxed == null ? false : (boolean)boxed;
    }
    

    so ,if you value is not base type,you can not direct use safeUnbox,you should define a static function to safe unbox by youself.

提交回复
热议问题