'Pass an Array to varargs parameter
If I have a function header like:
fun addAttributes(vararg attributes: String) {
...
}
And I want to pass attributes
in here:
val atts = arrayOf("1", "2", "3")
addAttributes(atts)
It gives a compilation error about incompatible types. What should I do?
Solution 1:[1]
you have two ways:
- using named parameter
addAttributes(attributes = arrayOf("1", "2", "3"))
- using spread operator
addAttributes(*arrayOf("1", "2", "3"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | amirhossein p |