无法推断当前上下文 RxSwift 中闭包参数的类型

Unable to infer type of a closure parameter in the current context RxSwift

我有这个功能

    func authorizeUser(user: User) -> Completable {
    let client = Client(name: user.username!, allowedGrantTypes: ["password", "refresh_token"])
    return self.userGateway.postClient(client: client)
        .flatMap { [weak self] authorizedClient in
             (self?.userGateway.getToken(authModel: AuthModel(
                                            clientId: "\(authorizedClient.id!)_\(authorizedClient.randomId!)",
                                            grantType: "password",
                                            username: user.username!,
                                            password: user.password!,
                                            clientSecret: authorizedClient.secret!)))!
        }.flatMap { token in
            self.settings.token = token
            return self.userGateway.getCurrentUser(token: token)
        }.map { user in
            self.settings.account = user
        }.asCompletable()
}

而且效果很好。但是当我试图将 AuthModel 作为变量并将其作为函数参数时,我收到错误“无法推断当前上下文中闭包参数的类型 'token'”。我做错了什么?我的错误函数如下所示

    func authroizeUser(user: User) -> Completable {
    let client = Client(name: user.username!, allowedGrantTypes: ["password", "refresh_token"])
    return self.userGateway.postClient(client: client)
        .flatMap { [weak self] authorizedClient in
            let authModel = AuthModel(
                clientId: "\(authorizedClient.id!)_\(authorizedClient.randomId!)",
                grantType: "password",
                username: user.username!,
                password: user.password!,
                clientSecret: authorizedClient.secret!)
            return (self.userGateway.getToken(authModel: authModel))!
        }.flatMap { token in
            self.settings.token = token
            return self.userGateway.getCurrentUser(token: token)
        }.map { user in
            self.settings.account = user
        }.asCompletable()
}

你没有做错任何事。这就是 Swift 的工作原理。一旦未知 return 类型的闭包长度超过一行,您必须明确地告诉 Swift 编译器它的 return 类型;编译器无法再通过查看闭包内容来推断它。所以就这么干吧。