How to use less than (<) or greater than (>) operator in kotlin?
I have checked comparedTo(other: Int?)
function, but it only returns Int?
.
class Adapter{
private var mNewsCategories: List<NewsCategory>? = null
//......
val isAnything= this.mNewsCategories?.size?.compareTo(0))
//......
}
The val isAnything
returns another Int?
. Actually, I need a Boolean
variable.
Thanks in advance
It’s not possible to use >
on nullable types. If you consider null
to map to the size 0
, i.e. empty size, you can do:
val isAnything = (this.mNewsCategories?.size? ?: 0) > 0
While this will fix your problem, you should consider using isNotEmpty
instead:
val isAnything = this.mNewsCategories?.isNotEmpty() ?: false
The Elvis Operator is explained here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With