如何在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
相关产品推荐
相关产品推荐

