gpt4 book ai didi

swift - RxSwift 通用 `Cannot convert value of type ' (_) -> [任何 ]' to expected argument type ' (字符串) -> _'`

转载 作者:行者123 更新时间:2023-11-30 12:04:09 25 4
gpt4 key购买 nike

我想创建 ObservableType 的扩展,但以下代码无法编译。

public extension ObservableType {
public func foo<C: Collection>(_ calls: @escaping (E) -> C) -> Observable<[Any]> where C.Iterator.Element: ObservableType {
return flatMap { input in Observable.zip(calls(input)) { objects in objects }
}
}
}

// compile error
Observable.just("foo")
.foo { foo in
[Observable.just(User()), Observable.just(Repo())]
}

它抛出一个错误:无法将类型“(_) -> [Any]”的值转换为预期参数类型“(String) -> _”

我不知道问题出在哪里。提前致谢

最佳答案

您的问题是您返回两种不同类型的数组 - UserRepo 。如果将返回值更改为这个,例如:

return [Observable.just(User()), Observable.just(User())] - 它将按预期工作。

更新:在您的情况下,您可以这样实现:

[Observable<Any>.just(User()), Observable<Any>.just(Repo())]

更新:使用以下返回类型更正确 foo功能-Observable<[C.Element.E]>恕我直言。

关于swift - RxSwift 通用 `Cannot convert value of type ' (_) -> [任何 ]' to expected argument type ' (字符串) -> _'`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860470/

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