Search code examples
swiftbindingreactive-programmingrx-swiftrx-cocoa

Two ways binding textfiled in RxSwift in different type


I am making two ways binding for UITextField extension. It works well with string. But why not Int? This is my extension:

import Foundation
import RxCocoa
import RxSwift

extension UITextField {
    func bind(with property: BehaviorRelay<String?>, bag: DisposeBag = DisposeBag()) {
        property.bind(to: self.rx.text).disposed(by: bag)
        self.rx.text.bind(to: property).disposed(by: bag)
    }

    func bind(with property: BehaviorRelay<Int?>, bag: DisposeBag = DisposeBag()) {
        property.map { intValue in
            return Int.toString(intValue) //convert from Int to String
        }.bind(to: self.rx.text).disposed(by: bag)
        self.rx.text.orEmpty.map { text in
            return String.toInt(text) ?? 0 //convert from String to Int
        }.bind(to: property).disposed(by: bag)
    }
}

In my viewModel:

let fullName = BehaviorRelay<String?>(value: nil)
let phone = BehaviorRelay<Int?>(value: nil)

Usage:

textField1.bind(with: viewModel.fullName) //working well
textField2.bind(with: viewModel.phone) //only from viewModel to textField

But when I use directly it working like this:

textField2.rx.text.map { text in
    return String.toInt(text) ?? 0
}.bind(to: viewModel.phone).disposed(by: bag)

Am I missing something? Any answer are appeciated. Thanks.


Solution

  • I missing pass bag when I call the function. Exactly, it should be:

    let bag = DisposeBag()
    textField1.bind(with: viewModel.fullName, bag: bag) //working well
    textField2.bind(with: viewModel.phone, bag: bag)
    

    Also I need to remove default value for avoiding forgetting in the future

    func bind(with property: BehaviorRelay<Int?>, bag: DisposeBag) {
            property.map { intValue in
                return Int.toString(intValue) //convert from Int to String
            }.bind(to: self.rx.text).disposed(by: bag)
            self.rx.text.orEmpty.map { text in
                return String.toInt(text) ?? 0 //convert from String to Int
            }.bind(to: property).disposed(by: bag)
        }