How to convert Any to Int in Kotlin
The problem is that you were attempting to cast directly from a String to an Int with value as Int
.
This isn't working because, as the exception is telling you, value
contains a String, and a String cannot be coerced into an Int. The fact that this String represents an integer doesn't matter, as it would need to be parsed into an Int. That precisely is what the toInt()
method does.
The reason you must cast to a String first is because toInt()
is an extension method on String, and value
is of type Any?
. This means that you can't call toInt()
directly on value
because it doesn't know that it contains a String at compile time, even though it does at runtime. If you wanted to skip this step, you could also use smart casts by checking the type first:
if (value is String) {
value.toInt()
}
You can find a bit more info on smart casts and other Kotlin type casting here: https://kotlinlang.org/docs/reference/typecasts.html#smart-casts
This also may be pointing to an underlying design problem. You expect that this will sometimes contain an Int, but as we can see here whatever is setting this value in your model is setting it to a String containing that number. Do you expect this value to truly be able to contain many different types, or are you only expect Int and String? If the latter, then it would possibly be a better design decision to make this a String rather than an Any?
. That would prevent unexpected/unhandled types from being provided, and make the model less ambiguous to anyone looking at or using it.
This is the way I do it:
val myInt = value as? Int ?: defaultValue
This will try to convert value to an int, but if it fails, will use defaultValue
instead.