【问题标题】:Cannot invoke 'filter' with an argument list of type '(@escaping (_) -> _)' within combineLatest in RxSwift无法在 RxSwift 的 combineLatest 中使用类型为“(@escaping(_)-> _)”的参数列表调用“过滤器”
【发布时间】:2020-01-23 15:30:55
【问题描述】:

我试图从我试图合并的两个流中过滤掉一些东西。我要尝试的两个值都属于同一类型,因此我似乎无法理解为什么会遇到此问题。

 let currentUserEmail = Observable.just(currentUserEmail)
            .unwrap()

//使用某种内部方式获取邮件的访问权限

let listOfAllUsers = Observable.combineLatest(getUsersList(), currentUserEmail) { allUsers, currentUserEmailAddress in
        return allUsers.filter { $0.emailAddress != currentUserEmailAddress } }
    .asObservable()
    .share(replay: 1, scope: .whileConnected)

【问题讨论】:

  • 您能否具体说明您的问题是什么,因为假设getUserList 返回类似于Observable<[User]>User 类似于struct User { let emailAddress } 的内容,您发布的代码将编译得非常好。跨度>
  • 我收到以下错误:无法使用类型为 '(@escaping (_) -> _)' 的参数列表调用 'filter' 而我不明白的是我' m 比较两种相同的类型
  • 我认为如果您发布编译示例所需的所有代码,详细帮助您会更容易,因此我们不会隐藏在细节中。例如。 getUsersList() 究竟返回了什么?你的 User 结构是什么样的?
  • 这段代码实际上是运行的,但我不确定为什么它对我不起作用,但由于某种原因,当我按照下面的方式执行它时它起作用了。此外,getUserList() 会附加很多代码,它是对网络请求的引用。

标签: swift filter rx-swift combinelatest


【解决方案1】:
let currentUserEmail = currentUserEmail

let listOfAllUsers = getUsersList()
       .asObservable()
       .share(replay: 1, scope: .whileConnected)

let newListOfAllUsersWithoutCurrentUser = listOfAllUsers.map { allUsers in
             allUsers.filter { $0.emailAddress != currentUserEmail }
         }
         .share(replay: 1, scope: .whileConnected)

这最终对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多