Skip to content

Instantly share code, notes, and snippets.

@sebreh
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebreh/56493d145b2aa125d837 to your computer and use it in GitHub Desktop.
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.
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