Latest 0.1.8
Homepage https://github.com/MaximKotliar/Bindy
License MIT
Platforms ios 9.0
Authors

Build status
Version
License
Platform

Just a simple bindings.

Installation

Add

pod 'Bindy'

to your podfile, and run
pod install

Usage

For now, bindy has a couple basic types

  • Signal – allows to trigger callback when some signal recieved.
  • Observable – allows to observe changing of value.
  • OptionalObservable – same as Observable, but with optional value.
  • ObservableArray – conforms to MutableCollection protocol, so you can work with it like with regular array: subscript index, replace objects, map, enumerate, etc… Also, ObservableArray has updates signal, which will notify you about any changes in array, such as insert, replace, delete.

Observables Sample

let firstname = Observable("Salvador")
let age = Observable(54)

func setupBindings() {
    age.bind(self) { [unowned self] newAge in
            print("Happy (newAge) birthday, (self.firstname.value)")
    }
    age.value = 55
}

Don’t forget always use [unowned owner] in closure to prevent retain cycle.

Signal and Array Sample

let messages: ObservableArray<Message> = []
let newMessage = Signal<Message>()

func setupBindings() {
    newMessage.bind(self) { [unowned self] message in
            self.messages.append(message)
    }

    messages.updates.bind(self) { [unowned tableView] updates in
            self.tableView.pefrom(updates: updates)     
       }
}

func handleDidRecieveMessage(_ message: Message) {
     newMessage.send(message)      
    }
}

You don’t need remove binding manually if you don’t want it, when object that you pass as owner in bind(_ owner: AnyObject... metod deallocates, corresponding bindings will clean. However, if you want to unbind manually, just call unbind(_ owner: AnyObject).
Bindy have an extension for tableView for performing updates tableView.perform(updates:...

Also, observables has method observe(_ owner: AnyObject..., it works like bind, but triggers callback immediately, this may be more comfortable in some situations.

Transformations

If you want to recieve events with transformed type, you can use transform function on Observables like:

let speed = Observable(20)
lazy var speedString = {
        speed.transform { "($0)km/h" }
}()

func setupBindings() {
    speedString.observe(self) { [unowned self] speedString in
        // speedString = "20km/h"
            self.speedLabel.text = speedString
        }
}

Combinations

You can combine two Observable types with combined(with: ..., transform: ...) function like:

let firstname = Observable("Maxim")
let lastname = Observable("Kotliar")
let age = Observable(24)

lazy var fullname = {
        return firstname
            .combined(with: lastname) { "name: ($0) ($1)" }
            .combined(with: age) { "($0), age: ($1)" }
}()

func setupBindings() {
    userInfo.observe(self) { [unowned self] info in
            // info = "name: Maxim Kotliar, age:24"
            self.userInfoLabel.text = info
        }
}

For Observable combinations Bindy have more convenient operators && and ||, so you can combine Observable like regular Bool, also you can invert it with !:

let isPremiumPurchased = Observable(true)
let isInTrialPeriodEnded = Observable(false)
let isAdsShowForced = Observable(false)

lazy var shouldShowAds = {
        return isAdsShowForced || !isPremiumPurchased && isInTrialPeriodEnded
}()

Latest podspec

{
    "name": "Bindy",
    "version": "0.1.8",
    "summary": "Simple, lightweight swift bindings.",
    "homepage": "https://github.com/MaximKotliar/Bindy",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Maxim Kotliar": "[email protected]"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/MaximKotliar/Bindy.git",
        "tag": "0.1.8"
    },
    "source_files": [
        "Classes",
        "Bindy/**/*.{h,m,swift}"
    ]
}

Pin It on Pinterest

Share This