后端返回可以为空的Int?
我该如何写出可以为空的值?
我该如何写出可以为空的值?
- date class Foo (var value: Int?){
- constructor(source: Parcel) : this(
- source.readInt()
- )
- override fun writeToParcel(dest: Parcel,flags: Int) {
- dest.writeInt(value) // doesn't compile - writeInt expect no null value
- }
- }
现在我得到解决方案:
- dest.writeInt(value?: -1)
然后检查-1
或者像字符串一样写Int然后使用值…
但我认为这是丑陋和错误的.
解决!我的答案:
- source.readValue(Int::class.java.classLoader) as Int?,dest.writeValue(value)
解决方法
解决方案:
- source.readValue(Int::class.java.classLoader) as Int?,dest.writeValue(value)