Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active March 19, 2023 10:14
Show Gist options
  • Save katsuyoshi/bb60560dab49591574b5e85229c75e0d to your computer and use it in GitHub Desktop.
Save katsuyoshi/bb60560dab49591574b5e85229c75e0d to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
import Combine
class MyViewController : UIViewController {
var label: UILabel!
var sub: AnyCancellable!
override func loadView() {
let view = UIView()
view.backgroundColor = .white
label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
let textField = UITextField()
textField.frame = CGRect(x:150, y: 300, width: 200, height: 20)
textField.text = "Hello World!"
textField.textColor = .black
sub = NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: textField)
.map( { ($0.object as! UITextField).text ?? "" } )
.filter( { $0!.unicodeScalars.allSatisfy({CharacterSet.alphanumerics.contains($0)}) } )
.debounce(for: .milliseconds(500), scheduler: RunLoop.main)
.receive(on: RunLoop.main)
.assign(to: \MyViewController.label.text, on: self)
view.addSubview(textField)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment