RxSwift上是否存在某种优先级运算符? [英] Is there some sort of Priority operator on RxSwift?

查看:139
本文介绍了RxSwift上是否存在某种优先级运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有2个验证规则的文本字段:最小字符数和字母数字字符。

I have a textfield that has 2 rules for validation: minimum amount of chars and alphanumerical chars.

我希望能够在错误标签中向用户表示他在做什么,但是问题是,如果我将文本字段绑定到两个规则,则可能会令人毛骨悚然因为一旦一个规则获得批准,ui就会从分隔符的颜色中稍微闪烁一下,例如由于另一个验证失败而从红色变为绿色再变为红色。

I want to be able to represent to the user what he's doing wrong in an error label but the problem is that if I bind the textfield to both rules it can be creepy because once one rule gets approved the ui does a little flickering from the color of the separator for example changing from red to green to red because of the other validation failing.

我想知道是否有一种方法可以使一个绑定优先于另一个绑定。例如,这就是我目前拥有的东西:

I'd like to know if there's a way to prioritize one bind over the other. For example, this is what I currently have:

let minimumValidator 
    = inputField.textField
      .rx.text.orEmpty.map { $0.count >= 8 } // Min amount of chars is 8

minimumValidator.bind(to: inputField.rx.minimumChars)
    .disposed(by: bag)

let regexValidator 
    = inputField.textField
      .rx.text.orEmpty.map { $0.matches(regex) }

regexValidator.bind(to: inputField.rx.regex)
    .disposed(by: bag)

rx.minimumChars和rx.regex是自定义活页夹

rx.minimumChars and rx.regex are custom binders

    var minimumChars: Binder<Bool> {
        return Binder(self.base) { control, value in
            UIView.animate(withDuration: 0.1) {
                if value {
                    control.separator.backgroundColor = .white
                    control.errorLabel.isHidden = true
                } else {
                    let error = "Needs more characters"
                    control.separator.backgroundColor = .red
                    control.errorLabel.text = error
                    control.errorLabel.isHidden = false
                }
            }
        }
    }

所以我的想法是优先考虑idk ...假设字母数字验证-这样它将显示从最小char直到出现字母数字错误的错误,因此直到用户解决了字母数字验证为止会忽略最小字符数的另一个流。我很确定我会缺少CombineLatest与merge或idk的某种组合。

So my idea is to prioritize idk... let's say alphanumerical validation - so it would show error from minimum char till alphanumerical error came up, so till the user resolved the alphanumerical validation it would ignore the other stream from minimum amount of chars. I'm pretty sure i'm missing some combination of combineLatest with merge or idk.

推荐答案

使单个活页夹使用自定义枚举

Make a single Binder that uses a custom enum

enum Validation {
    case valid
    case minimumChars
    case alphaNumeric
}

然后让每个验证器返回该枚举的值。然后合并两个验证器,并将结果映射到单个验证并绑定。

And then have each validator return a value of this enum. Then combineLatest the two validators and map the result to a single Validation and bind.

类似

Observable.combineLatest(minimumValidator, regexValidator) { v1, v2 in
    // if v1 is not valid return it. Otherwise return v2
}

这篇关于RxSwift上是否存在某种优先级运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆