Swift 平面地图不';不能被调用
我尝试使用两个单独的函数调用来验证用户的电子邮件和密码 这两个函数都返回AnyPublisher-publisher,我使用combineLatest将返回的值(每个validate调用返回它正在验证的字符串)收集到一个元组中 然后我使用flatMap发出网络请求,使用CombineTest返回的值注册用户,但是flatMap操作符从未被调用Swift 平面地图不';不能被调用,swift,combine,Swift,Combine,我尝试使用两个单独的函数调用来验证用户的电子邮件和密码 这两个函数都返回AnyPublisher-publisher,我使用combineLatest将返回的值(每个validate调用返回它正在验证的字符串)收集到一个元组中 然后我使用flatMap发出网络请求,使用CombineTest返回的值注册用户,但是flatMap操作符从未被调用 validator.validate(text: email, with: [.validEmail]) .combineLatest(validat
validator.validate(text: email, with: [.validEmail])
.combineLatest(validator.validate(text: password, with: [.notEmpty]))
.flatMap { credentials in
return self.userSessionRepository.signUp(email: credentials.0, password: credentials.1)
}
.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error):
print(error)
self.indicateErrorSigningIn(error)
case .finished:
self.goToSignInNavigator.navigateToOtp()
}
}, receiveValue: { _ in })
.store(in: &subscriptions)
注册(电子邮件:密码:)返回任何发布者
下面是验证器函数:
public func validate(text: String, with rules: [Rule]) -> AnyPublisher<String, ErrorMessage> {
rules.publisher
.compactMap { $0.check(text) }
.setFailureType(to: ErrorMessage.self)
.flatMap {
Fail<Void, ErrorMessage>(error: ErrorMessage(title: "Error", message: $0.description))
}
.map { text }
.eraseToAnyPublisher()
}
public func signUp(email: String, password: String) -> AnyPublisher<Void, ErrorMessage> {
remoteAPI.signUp(email: email, password: password)
.flatMap(dataStore.save)
.mapError { error -> ErrorMessage in
return ErrorMessage(title: "Error", message: error.description)
}
.eraseToAnyPublisher()
}
public func-validate(文本:字符串,带规则:[Rule])->AnyPublisher{
rules.publisher
.compactMap{$0.check(text)}
.setFailureType(收件人:ErrorMessage.self)
.平面图{
失败(错误:ErrorMessage(标题:“错误”,消息:$0.description))
}
.map{text}
.删除任何发布者()
}
以及注册功能:
public func validate(text: String, with rules: [Rule]) -> AnyPublisher<String, ErrorMessage> {
rules.publisher
.compactMap { $0.check(text) }
.setFailureType(to: ErrorMessage.self)
.flatMap {
Fail<Void, ErrorMessage>(error: ErrorMessage(title: "Error", message: $0.description))
}
.map { text }
.eraseToAnyPublisher()
}
public func signUp(email: String, password: String) -> AnyPublisher<Void, ErrorMessage> {
remoteAPI.signUp(email: email, password: password)
.flatMap(dataStore.save)
.mapError { error -> ErrorMessage in
return ErrorMessage(title: "Error", message: error.description)
}
.eraseToAnyPublisher()
}
public func注册(电子邮件:String,密码:String)->AnyPublisher{
注册(电子邮件:电子邮件,密码:密码)
.flatMap(dataStore.save)
.mapError{error->ERRORMASE中的错误消息
返回ErrorMessage(标题:“错误”,消息:Error.description)
}
.删除任何发布者()
}
它调用这两个函数:
public func signUp(email: String, password: String) -> AnyPublisher<Confirmation, RemoteError> {
guard email == "john.doe@email.com" else {
return Fail<Confirmation, RemoteError>(error: .invalidCredentials)
.eraseToAnyPublisher()
}
return Just(Confirmation(otp: "", nonce: "abcd"))
.setFailureType(to: RemoteError.self)
.eraseToAnyPublisher()
}
public func save(confirmation: Confirmation) -> AnyPublisher<Void, RemoteError> {
self.nonce = confirmation.nonce
return Empty().eraseToAnyPublisher()
}
public func注册(电子邮件:String,密码:String)->AnyPublisher{
警卫电子邮件==”约翰。doe@email.com“否则{
返回失败(错误:.invalidCredentials)
.删除任何发布者()
}
仅返回(确认(otp:,暂时:“abcd”))
.setFailureType(收件人:RemoteError.self)
.删除任何发布者()
}
public func save(确认:确认)->AnyPublisher{
self.nonce=confirmation.nonce
返回空()
}
我不确定出了什么问题,尽管可能是我的理解不够,因为我最近才开始学习 我已经弄明白了
问题在于validate(text:with:)函数
在发生错误的情况下,函数的行为是正确的,但当没有错误时,函数不会发出任何值,这就是为什么不会调用flatMap或管道中的任何其他操作符的原因
它没有发出任何值的原因归结为在compactMap中调用的check(:)函数的工作方式。它返回一个可选字符串,这是一条错误消息。但如果没有错误,就没有字符串,因此不会发出任何值
因此,对.map{text}的调用不会得到计算,也不会返回凭证
我已将代码更改为此,现在程序运行正常:
public func validate(text: String, with rules: [Rule]) -> AnyPublisher<String, ErrorMessage> {
rules.publisher
.setFailureType(to: ErrorMessage.self)
.tryMap { rule -> String in
if let error = rule.check(text) {
throw ErrorMessage(title: "Error", message: error)
}
return text
}
.mapError { error -> ErrorMessage in
return error as! ErrorMessage
}
.eraseToAnyPublisher()
}
public func-validate(文本:字符串,带规则:[Rule])->AnyPublisher{
rules.publisher
.setFailureType(收件人:ErrorMessage.self)
.tryMap{rule->输入字符串
如果let error=rule.check(文本){
抛出错误消息(标题:“错误”,消息:错误)
}
返回文本
}
.mapError{error->ERRORMASE中的错误消息
返回错误为!ErrorMessage
}
.删除任何发布者()
}