Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My ReactiveCocoa Swift support. Optionals in blocks.
//
// RACSupport.swift
//
// Created by Valentine on 19.11.14.
// Copyright (c) 2014 silvansky. All rights reserved.
//
import Foundation
// RAC
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)
}
infix operator ~> {}
func ~> (signal: RACSignal, rac: RAC) {
rac.assignSignal(signal)
}
// RACObserve
func RACObserve(target: NSObject!, keyPath: String) -> RACSignal {
return target.rac_valuesForKeyPath(keyPath, observer: target)
}
// RACSignal
extension RACSignal {
func mapAs<T: AnyObject, U: AnyObject>(mapClosure:(T?) -> U?) -> RACSignal {
return self.map {
(next: AnyObject!) -> AnyObject! in
if let _next: AnyObject = next {
let _nextAsT = _next as T
return mapClosure(_nextAsT)
}
return mapClosure(nil)
}
}
func filterAs<T: AnyObject>(filterClosure:(T?) -> Bool) -> RACSignal {
return self.filter {
(next: AnyObject!) -> Bool in
if let _next: AnyObject = next {
let _nextAsT = _next as T
return filterClosure(_nextAsT)
}
return filterClosure(nil)
}
}
}
extension RACSignal {
func subscribeNextAs<T>(nextClosure:(T?) -> ()) -> () {
self.subscribeNext {
(next: AnyObject!) -> () in
if let _next: AnyObject = next {
let _nextAsT = _next as T
nextClosure(_nextAsT)
} else {
nextClosure(nil)
}
}
}
func doNextAs<T: AnyObject>(nextClosure:(T?) -> ()) -> RACSignal {
return self.doNext {
(next: AnyObject!) -> () in
if let _next: AnyObject = next {
let _nextAsT = _next as T
nextClosure(_nextAsT)
} else {
nextClosure(nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.