如何在 Kotlin 中使用可变参数中的列表值。

huangapple go评论62阅读模式
英文:

How to use list value in vararg in kotlin

问题

以下是翻译好的代码部分:

我有一些键值对的值想要在 `vararg` 中使用值的格式类似于这样 [answer][1]

val verticalBrush2 = Brush.verticalGradient(
    0.25f to Color.Blue,
    1f to Color.Red,
)

我有一些业务逻辑需要使用不同的颜色所以我将它转换成了这样

val brushColors = listOf(0.25f to Color.Blue, 1f to Color.Red)

现在我将它传递给了 `drawLine` 函数的 brush 参数[参考链接][2]它的类型是 `vararg`我按照这个 [答案][3] 的方式将它转换为 `toTypedArray()`。

drawLine(
    brush = Brush.verticalGradient(
        listOf(0.25f to Color.Blue, 1f to Color.Red)
            .map { it }
            .toTypedArray()
    ),
    start = Offset(x = 0F, y = 0F),
    end = Offset(x = 0F, y = size.height),
    strokeWidth = 3.dp.toPx(),
)

但仍然报错

[![查看错误图片][4]][4]


  [1]: https://stackoverflow.com/a/76235677/8266651
  [2]: https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Brush#verticalGradient(kotlin.Array,kotlin.Float,kotlin.Float,androidx.compose.ui.graphics.TileMode)
  [3]: https://stackoverflow.com/a/65520425
  [4]: https://i.stack.imgur.com/kiTgx.png

请注意,我只翻译了代码部分,不包括问题的翻译。如果您需要问题的翻译,请提供问题的具体内容。

英文:

I have pairs value and want to use in vararg. The value is something like this answer

val verticalBrush2 = Brush.verticalGradient(
    0.25f to Color.Blue,
    1f to Color.Red,
)

I have some business logic to use different colors. So I converted into like this

val brushColors = listOf(0.25f to Color.Blue, 1f to Color.Red)

Now I passed in drawLine brush parameter which is type of vararg. I followed this answer to convert into toTypedArray().

                    drawLine(
                        brush = Brush.verticalGradient(
                            listOf(0.25f to Color.Blue, 1f to Color.Red)
                                .map { it }
                                .toTypedArray()
                        ),
                        start = Offset(x = 0F, y = 0F),
                        end = Offset(x = 0F, y = size.height),
                        strokeWidth = 3.dp.toPx(),
                    )

But still gives me error

如何在 Kotlin 中使用可变参数中的列表值。

答案1

得分: 2

你漏掉了扩展操作符。来自Kotlin文档

> 当你调用可变参数函数时,你可以逐个传递参数,例如asList(1, 2, 3)。如果你已经有一个数组并想将其内容传递给函数,请使用扩展操作符(在数组前加上*):

因此,你需要将你的列表转换为数组并且使用扩展操作符:

Brush.verticalGradient(*brushColors.toTypedArray())

你不需要.map { it },它不起作用。

英文:

You missed the spread operator. From Kotlin docs:

> When you call a vararg-function, you can pass arguments individually, for example asList(1, 2, 3). If you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with *):

So you have to convert your list to array and use the spread operator:

Brush.verticalGradient(*brushColors.toTypedArray())

You don't need .map { it }, it doesn't do anything.

huangapple
  • 本文由 发表于 2023年5月24日 18:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定