kotlin - 在 Kotlin 中获取可变参数的类型不匹配

标签 kotlin

这是我在代码中的简单情况

fun receiveDogs(vararg dog: Dog){
    processDogs(dog)
    ... //more cool stuff
}

fun processDogs(vararg dog: Dog){
  .....//cool stuff
}

当我这样做时调用 processDogs(dog)导致编译错误

Type mismatch.

Required: Dog

Found: Array<out Dog>

现在明白我的两个函数都需要可变参数。有解决此问题的简单方法吗?

最佳答案

哦……你需要做的

processDogs(*dog)

您需要再次将数组传播到可变参数中。

关于kotlin - 在 Kotlin 中获取可变参数的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712143/

相关文章:

kotlin - Ktor-静态内容路由

android - 拦截调用以刷新 token GRPC

kotlinx.serialization ,编码解码 SnapshotStateList

android - JUnit测试返回lateinit属性<nameOfInstance>尚未在演示者上初始化

java - 使用 viewModels 从 ChildFragment 访问 ParentFragment 中的 ViewModel

Kotlin 协程 : what's the diffrence between using NonCancellable and a standalone new Job

android - 可以将流从一个可组合传递到另一个吗?

android - 使用Firebase实时数据库

android - IllegalArgumentException : addAccount not supported

generics - 是否可以在泛型方法中指定任意可变参数列表?