Last active
December 12, 2017 22:54
-
-
Save demonar/d18326d6a34bc0ee875fece7747d6492 to your computer and use it in GitHub Desktop.
Reactive cocoa in swift 3/4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Always import | |
import ReactiveCocoa //Cocoa extensions | |
import ReactiveSwift //primitives | |
//Acces UI extensions with .reactive | |
self.textField.reactive. | |
//A text listener in a textfield | |
// self.reactive.lifetime will make the listener to expire on dealloc | |
self.textField.reactive.continuousTextValues.skip(first: 1).take(during: self.reactive.lifetime).observeValues { [weak self]text in | |
guard let myText = text, let this = self else { return } | |
} | |
//listen for notifications | |
NotificationCenter.default.reactive.notifications(forName: NSNotification.Name.UIKeyboardDidShow).take(during: self.reactive.lifetime).observe { [weak self]notification in | |
guard let this = self else { return } | |
//use the notification | |
} | |
//an Action on a button | |
self.closeButton.reactive.pressed = CocoaAction(Action<Void,Void,NoError> { [weak self] () in | |
self?.close() | |
return SignalProducer.empty | |
}) | |
//listen to a property in an object | |
let property = DynamicProperty<String?>(object: person, | |
keyPath: #keyPath(person.name)) | |
property.signal.skipNil().take(during: self.lifetime).observeValues { name in | |
} | |
//Mutable property | |
let mutableProperty = MutableProperty<Object?>(nil) | |
mutableProperty.value = Object() | |
mutableProperty.signal.skipNil().take(during: self.lifetime).observeValues { value in | |
} | |
//SignalProducer | |
let a = MutableProperty<String>("") | |
let b = MutableProperty<String>("") | |
let c = MutableProperty<String>("") | |
var disposable = SignalProducer.combineLatest(a.producer, b.producer, c.producer) | |
.startWithValues { aVal, bVal, cVal in | |
print("combined producer = \(aVal + bVal + cVal)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment