Ios ReactiveCocoa 3.0的mapAs、filterAs和subscribeNextAs
在Colin Eberhart的PDF上看到他自己做了这些扩展。他用Swift写过订阅,但其他的都没有 下面的答案正确吗Ios ReactiveCocoa 3.0的mapAs、filterAs和subscribeNextAs,ios,reactive-cocoa,reactive-cocoa-3,Ios,Reactive Cocoa,Reactive Cocoa 3,在Colin Eberhart的PDF上看到他自己做了这些扩展。他用Swift写过订阅,但其他的都没有 下面的答案正确吗 extension RACSignal { func subscribeNextAs<T>(nextClosure:(T) -> ()) -> () { self.subscribeNext { (next: AnyObject!) -> () in let nextAsT = n
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)
}
}
}
}
分机信号{
func subscribeNextAs(nextClosure:(T)->())->(){
self.subscribeNext{
(下一个:AnyObject!)->()中
设nextAsT=next as!T
nextClosure(nextAsT)
}
}
函数过滤器(下一个开关:(T!)->Bool->(RACSignal){
返回自滤波{
(下一步:AnyObject!)->Bool-in
if(next==nil){
返回nextClosure(无)
}否则{
设nextAsT=next as!T
返回nextClosure(nextAsT)
}
}
}
func映射(nextClosure:(T!)->AnyObject!)->(RACSignal){
返回self.map{
(下一步:AnyObject!)->AnyObject!在
if(next==nil){
返回nextClosure(无)
}否则{
设nextAsT=next as!T
返回nextClosure(nextAsT)
}
}
}
}
尽管您所写的似乎是正确的,但Colin Eberhart的这些扩展是很久以前为RAC v2开发的,当时RAC v3处于早期开发阶段。现在它已经是一个候选版本了,为什么不改用它呢?这很方便。Xcode将自动检测信号的类型
intSignal
|> filter { num in num % 2 == 0 }
|> map(toString)
|> observe(next: { string in println(string) })
更多信息请访问或wow sick!!“|>”是以前存在的所有冗长代码的语法糖吗!?非常感谢你回答我的问题。你似乎是ReactiveCocoaWell的大师,它不是一个语法糖,而是一个全新的架构。它不再是
RACSignal
,而是Signal
map
不再是返回其他信号的实例方法,而是返回闭包的全局函数|>
只是一个操作符,它将这个闭包包裹在接收器上。我只是在使用ReactiveCocoa:)questino@redection,因为发布候选版本集成到我的项目中真的很烦人。我按照他们的指示,使用xcode将.xcodeproject文件带到我的文件夹中,并尝试使用“常规”选项卡中的“嵌入二进制文件”添加ReactiveCocoa.framework,但它没有生成。。。。你是那样手工做的吗?使用迦太基要容易得多,但手工操作非常痛苦,我只使用迦太基来制作反应可可。无论如何,添加带有嵌入二进制文件的框架是很奇怪的,因为有“链接二进制文件和库”,不是吗?