Skip to content

Instantly share code, notes, and snippets.

@richy486
Created June 20, 2017 21:57
Show Gist options
  • Save richy486/847459eb20595c25e3f93c31bca45a6a to your computer and use it in GitHub Desktop.
Save richy486/847459eb20595c25e3f93c31bca45a6a to your computer and use it in GitHub Desktop.
Rx filter for text fields, doesn't really work with other observers
//
// UITextField+Rx+Extensions.swift
// ddxp-kiosk
//
// Created by Richard Adem on 6/20/17.
// Copyright © 2017 Richard Adem. All rights reserved.
//
import RxSwift
import RxCocoa
extension Reactive where Base: UITextField {
func filterInput(withMapFilter mapFilter: @escaping (String?) -> String?) -> Disposable {
let textValue = Variable<String?>("")
let bindToUIDisposable = textValue.asObservable()
.map(mapFilter)
.bind(to: self.text)
let bindToVariable = self.text
.subscribe(onNext: { str in
textValue.value = str
}, onCompleted: {
bindToUIDisposable.dispose()
})
return Disposables.create(bindToUIDisposable, bindToVariable)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment