I use ReactiveCocoa in many places around my application. I created a check to skip the values nilas follows:
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 record failed castings, but will also fail for values nil.
In Objective-C, you simply call ignore as shown below:
[[RACObserve(self, maybeNilProperty) ignore:nil] subscribeNext:^(id x) {
}];
But in Swift, the ignore property cannot be nil. Any idea to use ignore in Swift?
source
share