Rx swift 合并和枚举
Rx swift merge and enum
我收到 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
按钮,在此代码中我尝试使用 rx
绑定它
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 使用枚举函数映射值时,它们 return 4 值,以便我可以在其他流中使用它。问题是我不喜欢现在的情况。我认为 ts 应该是枚举应该有 4 个函数 return 作为其各自的值,例如 setTitle func return "Apple Line" (我还没有创建函数)。但是我如何将这个 func 设置到它的流,例如,如果它绑定到 lineName,地图应该调用 setTitle func?由于地图只有 return 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({ [=11=].cableName()})
.map({name in localized("label.powerBank") + name})
.bind(to: lineName)
.disposed(by: disposeBag)
我收到 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
按钮,在此代码中我尝试使用 rx
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 使用枚举函数映射值时,它们 return 4 值,以便我可以在其他流中使用它。问题是我不喜欢现在的情况。我认为 ts 应该是枚举应该有 4 个函数 return 作为其各自的值,例如 setTitle func return "Apple Line" (我还没有创建函数)。但是我如何将这个 func 设置到它的流,例如,如果它绑定到 lineName,地图应该调用 setTitle func?由于地图只有 return 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({ [=11=].cableName()})
.map({name in localized("label.powerBank") + name})
.bind(to: lineName)
.disposed(by: disposeBag)