Latest 1.6.1
License MIT
Platforms ios 8.0, osx 10.9


Observable is the easiest way to observe values in Swift.

How to

Create an Observable

var position = Observable(

Create an ImmutableObservable

Using ImmutableObservable we can create an "readonly"-Observable, in order to avoid side-effects on our internal API.

class SomeViewModel {
    /// Public property, that can be read / observed by external classes (e.g. view controller), but not changed.
    var position: ImmutableObservable<CGPoint> = {
        return positionSubject
    // Or use the helper method Observable.asImmutable()
    // lazy var position = positionSubject.asImmutable()

    /// Private property, that can be changed / observed inside this view model.
    private let positionSubject = Observable(

Create Observer with custom onDispose functionality

In some cases Observables require resources while they’re active that must be cleaned up when they’re disposed of. To handle such cases you can pass an optional block to the Observable initializer to be executed when the Observable is disposed of.

let observable = Observable([URL]()) {

Add an observer

position.observe { p in
    // handle new position

Add an observer and specify the DispatchQueue

position.observe(DispatchQueue.main) { p in
// handle new position

Change the value

position.value = p

Memory management

For a single observer you can store the returned Disposable to a variable

disposable = position.observe { p in

For multiple observers you can add the disposable to a Disposal variable

position.observe { }.add(to: &disposal)

And always weakify self when referencing self inside your observer

position.observe { [weak self] position in



Observable is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'Observable'

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

Latest podspec

    "name": "Observable",
    "version": "1.6.1",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    "summary": "The easiest way to observe values in Swift",
    "description": "Observable is the easiest way to observe values in Swift",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Robert-Hein Hooijmans": "[email protected]"
    "social_media_url": "",
    "source": {
        "git": "",
        "tag": "1.6.1"
    "source_files": "Observable/Classes/**/*.{swift}"

Pin It on Pinterest

Share This