RxSwift:无法转换类型'(_,_,Bool)的值 - > Bool'到预期的参数类型'(_,_,_) - > _”

时间:2017-03-12 12:49:44

标签: rx-swift

“signingIn:Observable”用于进度条,并阻止用户在我从API获得响应之前再次单击该按钮

let validatedUsername: Observable<Bool> 
let validatedPassword: Observable<Bool>
var signingIn : Observable<Bool>    // Is signing process in progress
let signupEnabled: Observable<Bool> //Is signup button enabled

validatedUsername = input.username
        .map { username in
            return username.characters.count > 5
        }
        .shareReplay(1)

validatedPassword = input.password
        .map { password in
            return  password.characters.count > 5
        }
        .shareReplay(1)

signupEnabled = Observable.combineLatest(
        validatedUsername,
        validatedPassword,
        signingIn.asObservable()
    )   { username, password , signing in  //the error at this line

        username.isValid &&
            password.isValid &&
            !signing
        }
        .distinctUntilChanged()
        .shareReplay(1)
}

1 个答案:

答案 0 :(得分:0)

由于validatedUsernamevalidatedPassword都属于Observable<Bool>类型,因此在.isValidusername上调用password可能不会进行类型检查。

错误最终在上面的行上,因为编译器无法推断返回类型并提前抛出某些内容。您可以通过输入传递给combineLatest的闭包的参数和返回类型来尝试检查是否收到更好的错误消息。

Observable.combineLatest(
        validatedUsername,
        validatedPassword,
        signingIn.asObservable()
    )   { (username: Bool, password: Bool, signing: Bool) -> Bool in    
  return username.isValid && password.isValid && !signing
}

现在抛出的错误应该更加明显,此修复可能与删除.isValid调用一样简单

return username && password && !signing