I'm using ReactiveCocoa in many places around my app. I've build a check to skip nil
values as followed:
func subscribeNextAs<T>(nextClosure:(T) -> ()) -> RACDisposable {
return self.subscribeNext {
(next: AnyObject!) -> () in
self.errorLogCastNext(next, withClosure: nextClosure)
}
}
private func errorLogCastNext<T>(next:AnyObject!, withClosure nextClosure:(T) -> ()){
if let nextAsT = next as? T {
nextClosure(nextAsT)
} else {
DDLogError("ERROR: Could not cast! \(next)", level: logLevel, asynchronous: false)
}
}
This helps to log failed castings, but will also fail for nil
values.
In Objective-C you would simply call ignore as followed:
[[RACObserve(self, maybeNilProperty) ignore:nil] subscribeNext:^(id x) {
// x can't be nil
}];
But in Swift the ignore property can't be nil
. Any idea to use ignore in Swift?