Swift implementation of the Observer Pattern taking full advantage of the latest language features to ensure type and thread safty. The library simplifies the syntax of notifications by hiding redundant service objects and automatically manages unsubscription of observers helping to avoid retain cycles which makes Reactive Programming in Swift easier than ever.
Open your application project in Xcode 11 or later, go to menu File -> Swift Packages -> Add Package Dependency...
and paste the package repository URL https://github.com/DnV1eX/CovertSwiftObserver.git
.
Add the pod to your Podfile
:
pod 'CovertSwiftObserver', '~> 1.0'
Or specify the git directly for the guaranteed latest version:
pod 'CovertSwiftObserver', :git => 'https://github.com/DnV1eX/CovertSwiftObserver.git'
Alternatively, you can manually copy CovertSwiftObserver.swift into your project (not recommended) or playground.
Cocoa's object keys observing technology.
A once useful class for sending notifications throughout the app.
A mechanism used by Controls to report events.
A template widely used in UIKit based on a weak object reference conforming to a predefined protocol.
Blocks of functionality with convenient syntax.
Comprehensive implementation of Functional Reactive Programming (FRP).
Long-awaited native implementation of FRP from Apple.
There are plenty of (functional) reactive programming libraries and frameworks for Swift out there. Most of them claim to be simple (or even the simplest) and provide excellent multi-page documentation. But you know what? You don't need any documentation to start using CovertSwiftObserver! 😎
class Drone {
@ObservedUpdate var altitude: Double = 0 // Observe value update
}
class Camera {
func refocus() { print("Camera refocused") }
}
class RemoteController {
var displayedAltitude: Double = 0 {
didSet { print("RC displayed altitude \(displayedAltitude)") }
}
}
let drone = Drone()
let camera = Camera()
let rc = RemoteController()
drone.$altitude.onUpdate.run { print("Drone altitude \($0)") } // Run closure
drone.$altitude.onUpdate.call(camera, Camera.refocus) // Call function
drone.$altitude.onUpdate.bind(rc, \.displayedAltitude) // Bind property
drone.altitude = 10
// Drone altitude 10.0
// Camera refocused
// RC displayed altitude 10.0
Copyright © 2018 DnV1eX. All rights reserved. Licensed under the Apache License, Version 2.0.
link |
Stars: 0 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics