solidcell solidcell - 1 year ago 221
Swift Question

Ignoring/filtering nil

Is there an operator that can filter

? The closest I've come is the solution mentioned here:

Relevant excerpt:

public protocol OptionalType {
func hasValue() -> Bool

extension Optional: OptionalType {
public func hasValue() -> Bool {
return (self != nil)

public extension ObservableType where E: OptionalType {
public func notNil() -> Observable<E> {
return self.filter { $0.hasValue() }

However, after
is still optional, so subsequent chained operators still see
is optional. So I'm still needing an extra operator that does:

.map { (username: String?) -> String in
return username!

I must be missing something. This seems like it would be a very common need.

Answer Source

checkout unwrap at :)


For now, you should use RxOptional for your personal needs
However, RxSwift-Ext will be growth exponentially in next 2-3 months :)

P/S: these guys (i'm talking about owners of these repos) chat with each others daily on RxSwift's slack :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download