如何返回可观察的< string>基于可观察的< bool> rxswift

发布于 2025-02-11 07:47:08 字数 807 浏览 3 评论 0原文

我是RXSWIFT的新手。

有什么方法我可以创建一个函数,该函数将根据下面的两个函数的条件返回可观察的函数,

func isValidPassword(_ str: PublishSubject<String>) -> Observable<Bool> {
    return str.asObservable().map {
        validator.isValidPasswordRegex($0) && $0.count >= 8
    }
}



func isNotEmpty(_ str: PublishSubject<String>) -> Observable<Bool> {
    return str.asObservable().map {
        $0.count != 0
    }
}

这只是我要实现的目标的一个示例,希望您能有这个想法。

func someFunc(_ str:PublishSubject<String>) -> Observable<String> {
   if !isValidPassword(str){
      return "Not a valid password" //should return as an observable string
   }
   if isNotEmpty(str){
      return "String is not empty" //should return as an observable string
   }
}

I'm quite new to RxSwift.

Is there any way I can create a function that will return Observable based on the conditions of two functions below

func isValidPassword(_ str: PublishSubject<String>) -> Observable<Bool> {
    return str.asObservable().map {
        validator.isValidPasswordRegex($0) && $0.count >= 8
    }
}



func isNotEmpty(_ str: PublishSubject<String>) -> Observable<Bool> {
    return str.asObservable().map {
        $0.count != 0
    }
}

This code below is just an example of what I'm trying to achieve, hoped you got the idea.

func someFunc(_ str:PublishSubject<String>) -> Observable<String> {
   if !isValidPassword(str){
      return "Not a valid password" //should return as an observable string
   }
   if isNotEmpty(str){
      return "String is not empty" //should return as an observable string
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

负佳期 2025-02-18 07:47:08

我不确定您在这里要实现的目标,但我认为您应该使用filter运算符,而不是map。这样,您就不会更改可观察的起始类型,而是删除不尊重您条件的元素。

func isValidPassword(_ str: String) -> Bool {
  validator.isValidPasswordRegex(str) && str.count >= 8
}

func someFunc(_ str: PublishSubject<String>) -> Observable<String> {
  str
    .filter(isValidPassword)
    .filter { !$0.isEmpty }
}

I'm not sure about what you want to achieve here but I think you should use filter operator and not map. In this way you don't change the type of the starting observable but you remove elements that don't respect your conditions.

func isValidPassword(_ str: String) -> Bool {
  validator.isValidPasswordRegex(str) && str.count >= 8
}

func someFunc(_ str: PublishSubject<String>) -> Observable<String> {
  str
    .filter(isValidPassword)
    .filter { !$0.isEmpty }
}
夏雨凉 2025-02-18 07:47:08

让我向您介绍ZIP ...它允许您组合多个可观察到的物品。

func someFunc(_ str: Observable<String>) -> Observable<String> {
    Observable.zip(isValidPassword(str), isNotEmpty(str))
        .map { isValidPassword, isNotEmpty -> String in
            if !isValidPassword {
                return "Not a valid password"
            }
            if isNotEmpty {
                return "String is not empty"
            }
            return "" // you didn't specify what it should return here...
        }
}

请注意,我更新了您现有功能的类型签名:

func isValidPassword(_ str: Observable<String>) -> Observable<Bool>
func isNotEmpty(_ str: Observable<String>) -> Observable<Bool>

通过这样的主题是灾难的秘诀。

受试者提供了一种方便的方法来戳RX,但是不建议它们日常使用。
- rx简介

更新

我认为,如果您实现这样的实现,则代码会更好:

func errorMessage(text: Observable<String>) -> Observable<String> {
    text.map(errorMessage)
}

func errorMessage(_ str: String) -> String {
    if !isValidPassword(str) {
        return "Not a valid password"
    }
    if isNotEmpty(str) {
        return "String is not empty"
    }
    return ""
}

func isValidPassword(_ str: String) -> Bool
func isNotEmpty(_ str: String) -> Bool

更易于测试要容易得多这边走。

Let me introduce you to zip... It allows you to combine multiple Observables.

func someFunc(_ str: Observable<String>) -> Observable<String> {
    Observable.zip(isValidPassword(str), isNotEmpty(str))
        .map { isValidPassword, isNotEmpty -> String in
            if !isValidPassword {
                return "Not a valid password"
            }
            if isNotEmpty {
                return "String is not empty"
            }
            return "" // you didn't specify what it should return here...
        }
}

Note that I updated the type signatures of your existing functions:

func isValidPassword(_ str: Observable<String>) -> Observable<Bool>
func isNotEmpty(_ str: Observable<String>) -> Observable<Bool>

Passing Subjects around like that is a recipe for disaster.

Subjects provide a convenient way to poke around Rx, however they are not recommended for day to day use.
-- Introduction to Rx

UPDATE

I think the code would be better if you implemented like this though:

func errorMessage(text: Observable<String>) -> Observable<String> {
    text.map(errorMessage)
}

func errorMessage(_ str: String) -> String {
    if !isValidPassword(str) {
        return "Not a valid password"
    }
    if isNotEmpty(str) {
        return "String is not empty"
    }
    return ""
}

func isValidPassword(_ str: String) -> Bool
func isNotEmpty(_ str: String) -> Bool

It's much easier to test this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文