You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kotlin/Native中将CValues转换为List或进行遍历?

Kotlin中CValues转List及遍历方法

直接遍历CValues对象

你可以通过索引范围直接遍历CValues,结合get方法访问每个元素:

val count = layerCount.value.toInt()
for (i in 0 until count) {
    val layerProp = layerProperties[i]
    // 处理单个VkLayerProperties,例:转换并打印层名称
    println(layerProp.layerName.toKString())
}

转换为Kotlin List

如果需要转为标准List,有两种简洁方式:

方法一:用buildList构建

val count = layerCount.value.toInt()
val layerList = buildList(count) {
    for (i in 0 until count) {
        add(layerProperties[i])
    }
}

方法二:结合indices与map

val count = layerCount.value.toInt()
val layerList = (0 until count).map { layerProperties[it] }

注意:VkLayerProperties中的字符串字段(如layerName)是C风格字符串,需调用toKString()才能转为Kotlin可用的字符串。

内容的提问来源于stack exchange,提问作者xdevs23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:49:51