【发布时间】:2015-10-17 00:06:12
【问题描述】:
在 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)
}
}
}
}
【问题讨论】:
标签: ios reactive-cocoa reactive-cocoa-3