Question, I have ``` fun getString(@StringRes r...
# android
d
Question, I have
Copy code
fun getString(@StringRes resId: Int, vararg formatArg:Any) = activity()?.getString(resId, formatArg)
So I can call this instead of needing the context in every screen. But it doesn't correctly format, and it just displays
lang.Object@zlkjdf
If I change the method to Java, it works though. Is some conversion going wrong from Kotlin to Java?
t
A
vararg Any
parameter is compiled into an
Array<Any>
. That's the issue here: when passing the array to another vararg function, the array is passed as-is, hence the
lang.Object
which is the string representation of the array in Java (not its content as a string!) In such case, you need to spread that array:
getString(resId, *formatArg)
(mind the * !)
👍 1
d
Thank you!
210 Views