ReactiveCocoa ignores zero in Swift

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) {
  // x can't be nil
}];

But in Swift, the ignore property cannot be nil. Any idea to use ignore in Swift?

+4
source share
1 answer

Finally, using powerj1984, I created this method for now:

extension RACSignal {
    func ignoreNil() -> RACSignal {
        return self.filter({ (innerValue) -> Bool in
            return innerValue != nil
        })
    }
}
+2
source

All Articles