When it comes to limiting the number of words in an input field, one of the things that comes to mind is textFieldDelegate’s proxy method. So if you’re using RxSwift, how do you do that? Let’s go straight to the code:

let countValid = priceField.rx.text.orEmpty.map { text -> Bool in
            text.count > 10
        }.share(replay: 1)


        countValid.subscribe(onNext: { valid in
            if  valid {
                letindex = self.priceField.text! .index(self.priceField.text! .startIndex, offsetBy: 10) self.priceField.text = String(self.priceField.text! [..<index]) } }).disposed(by: bag)Copy the code

How about that? Isn’t that easier?