Last active
May 3, 2022 06:19
-
-
Save wotjd/79fbb3d9d7307a0b9ba0330c75778cc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import RxCocoa | |
@dynamicMemberLookup | |
protocol DynamicMemberLookupableObservableType: AssociateObservable { | |
subscript<T>(dynamicMember keyPath: KeyPath<Value, T>) -> Observable<T> { get } | |
} | |
extension DynamicMemberLookupableObservableType { | |
subscript<T>(dynamicMember keyPath: KeyPath<Value, T>) -> Observable<T> { | |
self.myself.map({ $0[keyPath: keyPath] }) | |
} | |
} | |
extension Observable: DynamicMemberLookupableObservableType { } | |
@dynamicMemberLookup | |
protocol DynamicMemberLookupableBinder { | |
associatedtype Element | |
subscript<T>(dynamicMember keyPath: ReferenceWritableKeyPath<Element, T>) -> Binder<T> { get } | |
} | |
extension Reactive: DynamicMemberLookupableBinder where Base: AnyObject { | |
typealias Element = Base | |
subscript<T>(dynamicMember keyPath: ReferenceWritableKeyPath<Element, T>) -> Binder<T> { | |
Binder(self.base) { base, value in | |
base[keyPath: keyPath] = value | |
} | |
} | |
} | |
// usage | |
Observable.just("ABC").first // Observable<Character?> | |
SomeButton.rx.isHighlighted // Binder<Bool> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment