gpt4 book ai didi

android - 类型推断失败 Kotlin rxjava 映射使用

转载 作者:行者123 更新时间:2023-11-29 23:27:28 25 4
gpt4 key购买 nike

尝试添加一个 compositeDisposable。使用以下代码:

 compositeDisposable.add(
animalsObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.filter { s -> s.toLowerCase().startsWith("c") }
.map(object : Function<String, String>()
{
@Throws(Exception::class)
fun apply(s: String): String
{
return s.toUpperCase()
}
})
.subscribeWith(animalsObserverAllCaps)
)
}

但是,我收到以下错误:

类型推断失败:fun map(p0: Function!): Observable!不能应用于()
类型不匹配:推断类型只是函数!意料之中
接口(interface)函数需要一个类型参数

最佳答案

我希望它像这样简单:

.map { it.toUpperCase() }

代替

.map(object : Function<String, String>()
{
@Throws(Exception::class)
fun apply(s: String): String
{
return s.toUpperCase()
}
})

但无论如何,您可能没有使用 io.reactivex.functions.Function,因此会出现错误。如果您真的不想使用 lambda,那么:

.map(object : io.reactivex.functions.Function<String, String> {
override fun apply(t: String): String {
return t.toUpperCase()
}
})

或者只导入 io.reactivex.functions.Function 然后使用 Function

关于android - 类型推断失败 Kotlin rxjava 映射使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com