Last active
August 29, 2015 14:13
-
-
Save sebreh/56493d145b2aa125d837 to your computer and use it in GitHub Desktop.
Swift extensions to ReactiveCocoa to make it easier to work with from Swift.
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 Foundation | |
extension RACSignal { | |
func subscribeNextAs<T>(nextClosure: (T?) -> Void) { | |
self.subscribeNext { | |
(next: AnyObject!) -> () in | |
let nextAsT = next as? T | |
nextClosure(nextAsT) | |
} | |
} | |
func doNextAs<T>(nextClosure: (T?) -> Void) -> RACSignal { | |
return self.doNext { | |
(next: AnyObject!) -> () in | |
let nextAsT = next as? T | |
nextClosure(nextAsT) | |
} | |
} | |
} | |
struct RAC { | |
var target : NSObject! | |
var keyPath : String! | |
var nilValue : AnyObject! | |
init(_ target: NSObject!, _ keyPath: String, nilValue: AnyObject? = nil) { | |
self.target = target | |
self.keyPath = keyPath | |
self.nilValue = nilValue | |
} | |
func assignSignal(signal : RACSignal) { | |
signal.setKeyPath(self.keyPath, onObject: self.target, nilValue: self.nilValue) | |
} | |
} | |
infix operator <~ {} | |
func <~ (rac: RAC, signal: RACSignal) { | |
rac.assignSignal(signal) | |
} | |
func RACObserve(target: NSObject!, keyPath: String) -> RACSignal { | |
return target.rac_valuesForKeyPath(keyPath, observer: target) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment