swift - Rx 快速合并和枚举

标签 swift rx-swift

iam 给 rx swift 的消息,我真的需要帮助。 首先我有一个名为 Cable 的枚举

enum Cable {
case iphone
case android
case typec

func setProperty() -> (String, String, UIImage, UIImage) {

    switch self {
    case .iphone:
        return ("Apple line"  , "1" , #imageLiteral(resourceName: "icon_borrow_tiv_iphone") , #imageLiteral(resourceName: "icon_borrow_t_iphone"))
    case .android:
        return ("Android line", "2" , #imageLiteral(resourceName: "icon_borrow_tiv_android") , #imageLiteral(resourceName: "icon_borrow_t_android"))
    case .typec:
        return ("Type-C line" , "3" , #imageLiteral(resourceName: "icon_borrow_tiv_typec") , #imageLiteral(resourceName: "icon_borrow_t_typec"))
    }
 }

}

我有3个按钮,我们将其称为iphone按钮、android按钮和typec按钮,在这段代码中我试图绑定(bind)它使用接收器

private func bindActions() {
let tapStream = Observable.of(
              appleBtn.rx.tap.map  ({Cable.iphone.setProperty()}),
              androidBtn.rx.tap.map({Cable.android.setProperty()}),
              typeCBtn.rx.tap.map  ({Cable.typec.setProperty()}))
.merge()

tapStream.map({ name, type, image1, image2 in localized("label.powerBank") + name })
    .do(onNext: {[weak self] type in
     self?.borrowBtn.isEnabled = true})
    .bind(to: lineName)
    .disposed(by: disposeBag)

tapStream.map({ name, type, image1, image2 in type})
    .bind(to: cableType)
    .disposed(by: disposeBag)

tapStream.map({ name, type, image1, image2 in image1})
    .bind(to: powerBankImage.rx.image)
    .disposed(by: disposeBag)

tapStream.map({ name, type, image1, image2 in image2})
    .bind(to: cableImage.rx.image)
    .disposed(by: disposeBag)
}

当 applebtn、androidBtn、typeCbtn 使用枚举函数映射值时,它们返回 4 个值,以便我可以在其他流中使用它。问题是我不喜欢现在的情况。我认为 ts 应该是枚举应该有 4 个函数返回其各自的值,例如 setTitle func 返回“Apple Line”(我还没有创建该函数)。但是我如何将此函数设置为其流,例如如果它绑定(bind)到 lineName ,则 map 应调用 setTitle 函数?由于map只返回1个值,即合并时的名称。请帮助我有点迷路

最佳答案

在我了解有关 rx 和 enum 的更多信息后终于找到了我正在寻找的答案 所以枚举结构变成这样。

enum Cable{
case iphone
case android
case typec

func cableName() -> (String){

    switch self {
    case .iphone:
        return ("Apple line")
    case .android:
        return ("Android line")
    case .typec:
        return ("Type-C line")
    }
}

func cableType() -> (String){

    switch self {
    case .iphone:
        return ("1")
    case .android:
        return ("2")
    case .typec:
        return ("3")
    }
}

func powerBankImage() -> (UIImage) {

    switch self {
    case .iphone:
        return #imageLiteral(resourceName: "icon_borrow_tiv_iphone")
    case .android:
        return #imageLiteral(resourceName: "icon_borrow_tiv_android")
    case .typec:
        return #imageLiteral(resourceName: "icon_borrow_tiv_typec")
    }
}

func cableImage() -> (UIImage){

    switch self {
    case .iphone:
        return #imageLiteral(resourceName: "icon_borrow_t_iphone")
    case .android:
        return #imageLiteral(resourceName: "icon_borrow_t_android")
    case .typec:
        return #imageLiteral(resourceName: "icon_borrow_t_typec")
    }
}
}

之后你只需要对选定的函数进行元组

 tapStream.map({ $0.cableName()})
    .map({name in localized("label.powerBank") + name})
    .bind(to: lineName)
    .disposed(by: disposeBag)

关于swift - Rx 快速合并和枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48384278/

相关文章:

swift - 导入的框架找不到应该在其中的框架

ios - fatal error : Array index out of range (Parse/Swift)

swift - 如何正确地将 3rd 方库委托(delegate)转换为 RxSwift Observable

swift - Rx swift : Zip Observables only if requirements are met

ios - 每次 UITextField 文本属性在 RxSwift 中更改时如何获得信号

swift - 尾随闭包语法如何与 PublicSubject<Void>.subscribe 一起使用?

swift - 如何理解这个rx-swift的map函数

swift - 何时在 swift 中使用三种不同形式的关键字 "as"

ios - 标题标签文本颜色在 UIButton 中无法正确设置动画

ios - for循环的swift 3.0索引1超出范围[0 .. 0]错误