Skip to content

Instantly share code, notes, and snippets.

@kyungpyoda
Created June 1, 2021 00:36
Show Gist options
  • Save kyungpyoda/f051d869db81552343b78cedce2ddeba to your computer and use it in GitHub Desktop.
Save kyungpyoda/f051d869db81552343b78cedce2ddeba to your computer and use it in GitHub Desktop.
[Swift] Dynamic class for binding
//
// Dynamic.swift
//
// Created by 홍경표 on 2021/05/23.
//
import Foundation
final class Dynamic<T> {
typealias Listener = (T) -> Void
var listener: Listener?
var value: T {
didSet {
listener?(value)
}
}
init(_ v: T) {
value = v
}
func bind(_ listener: Listener?) {
self.listener = listener
}
func bindAndFire(_ listener: Listener?) {
self.listener = listener
listener?(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment