Swiftpack.co - Package - vknabel/RxLens

CocoaPods CocoaPods Install License


Enables reactive read and copy-on-write access for an entity's property in a data structure.

Check out the generated docs at vknabel.github.io/RxLens.


First we need to declare our data structure and an according lens for our property. An explanation for lenses can be found in this blog post of Chris Eidhof.

struct Person: Equatable {
    let name: String

    static var name: Lens<Person, String> {
        return Lens(
            from: { $0.name },
            to: { name, _ in Person(name: name) }

Lens Subject

You may create a subject that will only reflect changes of the name and allows pushing new names.

let personBehavior = BehaviorSubject(value: Person(name: "Valentin Knabel"))
let nameSubject = LensSubject(subject: personBehavior, lens: Person.name)


The following operators will be defined by using the following subject.

let personSubject = PublishSubject<Person>()

First of all you can use your lenses in order to access properties of your data structure with Observable.from(_:).

let nameSubject = personSubject.asObservable().from(Person.name)

Additionally you can use Observer.to(_:, with:) in order to only observe changes of your property.

let nameObserver = personSubject.asObserver().to(Person.name, with: Person(name: "Initial"))


RxLens is a Swift only project and supports Swift Package Manager and CocoaPods.

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .Package(url: "https://github.com/vknabel/RxLens.git", majorVersion: 1)


source 'https://github.com/CocoaPods/Specs.git'

pod 'RxLens', '~> 0.1'


Valentin Knabel, dev@vknabel.com


EasyInject is available under the MIT license. See the LICENSE file for more info.


Stars: 2
Help us keep the lights on

Used By

Total: 0


0.1.2 - Nov 15, 2016

Released: 2016-11-15


  • Added Lens.child(with:) - @vknabel
  • Added Lens.child(from:,to:) - @vknabel
  • Added Lens.asChild(of:) - @vknabel
  • Added Lens.asChild(from:,to:) - @vknabel

0.1.1 - Nov 6, 2016

Released: 2016-11-06

Further changes

  • Fixes wrong License name - @vknabel

0.1.0 - Nov 6, 2016