/Observable.Swift

A small piece of code to enable reactive programming into your project.

Primary LanguageSwift

Observable.Swift

This is a work in progress.

With this small piece of code (Observable.swift) you will be able to add reactivity programming to your app. Simple, useful and easy to use.

var n = Observable(1)
var c: Int = 0

n.valueDidChange = {
    c = n.value // 2 then 3
}

n.value = 2
c // 2
n.value = 3
c // 3

For more features like ObservableTextField take a look at the example project.

ObservableSwitch:
With the ObservableSwitch you are able to combine signals and run some code depending on the status result. Bellow we enable/disable a UIButton based on the length of the username and password fields.

let obSwitch = ObservableSwitch()
obSwitch.action = { (status: Bool) -> () in
    self.signin.alpha   = (status) ? 1 : 0.5
    self.signin.enabled = status
}

// ObservableTextField properties
self.username.addSignal({ self.username.count >= 4 }, toSwitch: obSwitch)
self.password.addSignal({ self.password.count >= 4 }, toSwitch: obSwitch)

Alt text

ObservableTextField:

self.textField.textDidChange = {(text: String) -> () in
	self.label.text = "Hi \(text)!"
}

Alt text

Observable:

self.word.valueDidChange = {
    self.label.text = self.word.value
    self.label.textColor = self.getRandomColor()
}

Alt text

License

All this code is released under the MIT license.