将Kotlin中的数据类值转换为不同类型。

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

Convert data class value into different in kotlin

问题

I have a data class like this:

data class BrushStopColors(
    val stopPoint: Float,
    val color: Int,
)

I have a list:

val brushStopColors = listOf(
    BrushStopColors(0.3F, 1),
    BrushStopColors(1F, 1),
)

I want to convert my color from Int to String or any other format. So I tried this code:

val brushArray = brushStopColors.map { it.color.toString() }

And want to change brushArray to .toTypedArray() and pass it into a different function. It looks like this:

fun verticalGradient(vararg colorStops: Pair<Float, String>) {
    // more code here
}

When I pass the array, it's giving me an error:

val brushArray = brushStopColors.map { it.color.toString() }.toTypedArray()
verticalGradient(*brushArray)

Error:

Type mismatch.
Required:
Array<out Pair<Float, String>>
Found:
Array

Image:

将Kotlin中的数据类值转换为不同类型。

英文:

I have a data class like this

data class BrushStopColors(
    val stopPoint: Float,
    val color: Int,
)

I have a list

val brushStopColors = listOf(
        BrushStopColors(0.3F, 1),
        BrushStopColors(1F, 1),
    )

I want to convert my color from Int to String or any other format. So I tried this code

val brushArray = brushStopColors.map { it.color.toString() }

And want to change brushArray to .toTypedArray() and passing into different function. It looks like this

fun verticalGradient(vararg colorStops: Pair&lt;Float, String&gt;){
   /// more code in here
}

When I passed the array it's giving me error

val brushArray = brushStopColors.map { it.color.toString() }.toTypedArray()
verticalGradient(*brushArray)

Error

Type mismatch.
Required:
Array&lt;out Pair&lt;Float, String&gt;&gt;
Found:
Array&lt;String&gt;

Image

将Kotlin中的数据类值转换为不同类型。

答案1

得分: 1

The above returns a list of String instead of a list of Pair.

请使用以下代码:

fun BrushStopColors.toPair(): Pair<Float, String> = Pair(this.stopPoint, this.color.toString())

val brushArray = brushStopColors.map { it.toPair() }.toTypedArray()

或者更加简洁:

val brushArray = brushStopColors.map { Pair(it.stopPoint, it.color.toString()) }.toTypedArray()
英文:
val brushArray = brushStopColors.map { it.color.toString() }

The above returns a list of String instead of a list of Pair.

Please use this:

fun BrushStopColors.toPair(): Pair&lt;Float, String&gt; = Pair(this.stopPoint, this.color.toString())


val brushArray = brushStopColors.map { it.toPair() }.toTypedArray()

Or even more concise :

val brushArray = brushStopColors.map { Pair(it.stopPoint, it.color.toString()) }.toTypedArray()

答案2

得分: 1

如果您打印brushArray,您将看到它将整个对象映射到一个字符串。我会使用这样的扩展函数:

fun BrushStopColors.toGradientPair() = Pair<Float, String>(this.stopPoint, this.color.toString())
英文:

If you print the brushArray you'll see that it maps the entire object to a String. I would use an extension function like this:

fun BrushStopColors.toGradientPair() = Pair&lt;Float, String&gt;(this.stopPoint, this.color.toString())

huangapple
  • 本文由 发表于 2023年5月25日 21:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332696.html
匿名

发表评论

匿名网友

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

确定