在 Colin Eberhart 的 pdf 中看到他自己做了这些扩展。他在 Swift 中写过 subscribeNextAs,但没有写过其他任何一个。
下面是正确的吗?
extension RACSignal {
func subscribeNextAs<T>(nextClosure:(T) -> ()) -> () {
self.subscribeNext {
(next: AnyObject!) -> () in
let nextAsT = next as! T
nextClosure(nextAsT)
}
}
func filterAs<T>(nextClosure:(T!) -> Bool) -> (RACSignal) {
return self.filter {
(next: AnyObject!) -> Bool in
if(next == nil){
return nextClosure(nil)
}else{
let nextAsT = next as! T
return nextClosure(nextAsT)
}
}
}
func mapAs<T>(nextClosure:(T!) -> AnyObject!) -> (RACSignal) {
return self.map {
(next: AnyObject!) -> AnyObject! in
if(next == nil){
return nextClosure(nil)
}else{
let nextAsT = next as! T
return nextClosure(nextAsT)
}
}
}
}