【发布时间】:2015-09-21 03:11:30
【问题描述】:
我在RACSignal 上为许多常见的 ReactiveCocoa 操作写了一个扩展,map、filter、subscribeNext,以便我可以在回调块中显式指定类型。 map 变成 mapAs,filter 变成 filterAs 和 subscribeNext 变成 subscribeNextAs(等等)
func subscribeNextAs<T>(nextClosure:(T) -> ()) -> RACDisposable! {
return self.subscribeNext {
(next) -> () in
if let nextAsT = next as? T {
nextClosure(nextAsT)
}
}
}
然而,我注意到的一个问题是可选值没有传递给nextClosure,这是正确的,因为if let nextAsT 失败了。
我如何重写这个扩展函数,以便subscribeNextAs 允许我同时转换可选和非可选?
例子:
RACObserve(someObject, potentiallyOptionalTitle).subscribeNextAs({
(next: String?) in
})
RACObserve(someObject, nonOptionalTitle).subscribeNextAs({
(next: String) in
})
【问题讨论】:
标签: ios swift reactive-cocoa