1

I am trying to get Formatting String in AndroidViewModel by using following extension function but failed:

<string name="version_text">Version %1$s</string>

val versionText = getString(R.string.version_text, "1.0.0")

fun AndroidViewModel.getString(resId: Int, vararg formatArgs: Any) = (getApplication() as Context).getString(resId, formatArgs)

Result:

Version [Ljava.lang.Object;@bad21e0

1 Answer 1

1

vararg parameters are Array types. Use the * operator to expand them:

(getApplication() as Context).getString(resId, *formatArgs)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.