0

I'm trying to add delay into UITextField but I'm getting the following error:

Property 'text' requires that 'UITextField' inherit from 'UILabel'
Value of type 'Binder<String?>' has no member 'debounce'

Here is my implementation:

   func bind() {
        (myTextField.rx.text.debounce(0.5, scheduler: MainScheduler.instance) as AnyObject)
            .map {
                if  $0 == ""{
                    return "Type your name bellow"
                }else {
                    return "Hello, \($0 ?? "")."
                }
        }
        .bind(to: myLbl.rx.text)
        .disposed(by: disposeBag)
    }

Any of you knows why I'm getting this error?

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

0

myTextField.rx.text is a ControlProperty<String?>, and sometimes long chains can get the Swift compiler to have trouble distinguishing what are you trying to accomplish. It's better to declare your intent and split long chains into variables:

func bind() {
    // The way you wanted to do it
    let property: ControlProperty<String> = _textField.rx.text
        .orEmpty
        // Your map here

    property
        .debounce(.milliseconds(500), scheduler: MainScheduler.instance)
        .bind(to: _descriptionLabel.rx.text)
        .disposed(by: _disposeBag)

    // Driver is a bit better for UI
    let text: Driver<String> = _textField.rx.text
        .orEmpty
        // Insert your map here
        .asDriver()

    text
        .debounce(.milliseconds(500))
        .drive(_descriptionLabel.rx.text)
        .disposed(by: _disposeBag)
}

P.S. Using a Driver will save you some typing for UI and make it a bit clearer.

Adis
  • 4,512
  • 2
  • 33
  • 40