Latest0.1.0
Homepagehttps://github.com/MenhSowattana/ReactiveZ
LicenseMIT
Platformsios 8.0
Authors

Lightweight Reactive programming library in swift. It provides bi-directional binding for UIControls.

Installation

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

pod 'ReactiveZ'

Usage

ObservableField

it makes variable with any types to be observable.

let name: ObservableField<String> = ObservableField("")
let age: ObservableField<Int> = ObservableField(0)
let money: ObservableField<Double> = ObservableField(0.0)
let contacts: ObservableField<[String]> = ObservableField([])

You can also write in short as :

let name = ObservableField("")

it also support with custom object and optional wrapping.

let student: ObservableField<Student?> = ObservableField(nil)

in order to get value from ObservableField, we use:

print(name.get())

in order to set value into ObservableField, we use:

name.set(value: "Example")

Observer

It is used to observe the Observable Type.

let nameObserver = Observer<String>()
let ageObserver = Observer<Int>()

Binding & Observing

Observing is one-directional binding which observer can only observe the value changing of a observable type.

nameObserver.observe(observable: name)

Binding is bi-directional binding which observer can observe and change the value of a observable type.

nameObserver.bind(to: name)

You can annonymously observe the ObservableField object without creating observer.

name.observe { (value) in
    print(value)
}

Notice : One ObservableField can be observed by many observers. One Observer can only observe or bind with only one ObservableField.

Update & Get Value

To update value to:

nameObserver.update(value: "Tata")

To get value:

print(nameObserver.get())

Binding with UIControls

When use with UIControls, we use rz property to simplify binding. Currently, we can only support controls as below:

UIControlsBinding Property
UITextFieldtext
UILabeltext
UIImageViewimage
UIButtontitle

Usage

let disposal = Disposal()
textField.rz.bind(to: name, disPosal: disposal) // name is ObservableField

Notice : When you bind with UIControl, Disposal is needed to prevent memory leak.

You have to call dispose when you stop using the observer.

deinit {
   disposal.dispose()
}

Map Value

Map value is needed when you bind Observer and ObservableField with different type.

let age: ObservableField<Int> = ObservableField(0)
let ageMessageObserver = Observer<String>()

ageMessageObserver.bind(to: age, map: { (value) -> String in
    return "My age is (value)"
}, mapBack: { (value) -> Int in
    return value == "reset" ? 0 : 20
})

Latest podspec

{
    "name": "ReactiveZ",
    "version": "0.1.0",
    "summary": "This is a beginning of lightweight reactive programming.",
    "description": "Lightweight Reactive programming library in swift. With this libray you can bind control with observable field to make it reactive.",
    "homepage": "https://github.com/MenhSowattana/ReactiveZ",
    "license": "MIT",
    "authors": {
        "Sowattana": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/MenhSowattana/ReactiveZ.git",
        "tag": "0.1.0"
    },
    "source_files": "ReactiveZ/**/*.swift"
}

Pin It on Pinterest

Share This