11

I am not so convinced with RxSwift yet, and it's really hard to cleat understanding. After reviewing different materials, I cant' still work and manipulate sequences. On the whole I have problem with type converting:

Cannot convert return expression of type 'Observable<Bool>' to return type 'Observable<Void>' (aka 'Observable<()>')    

I have CocoaAction processing, and should return Observable<Void>

func stopProject() -> CocoaAction {
    return CocoaAction { _ in
        let stopProject = stop(project: self.projectId)
        return stopProject.asObservable() //wrong converting
    }
}

The stop function return Observable<Bool>

Finish view:

    return stop(project: self.projectId).flatMap { _ in
        Observable<Void>.empty()
    }
biloshkurskyi.ss
  • 1,358
  • 3
  • 15
  • 34

3 Answers3

16
let voidObservable = boolObservable.map { Void() }
Maxim Volgin
  • 3,957
  • 1
  • 23
  • 38
  • Yes, in some case you are right. I have found hint in https://github.com/ReactiveX/RxSwift/issues/1206 – biloshkurskyi.ss Aug 17 '17 at 14:51
  • 1
    The following code worked for us : .map { _ in Void() }, it looks like you may need empty parameters, the code you mentioned doesn't work and fails with : 'Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored" – Peter Suwara Jan 21 '19 at 01:29
3

let voidObservable = boolObservable.map { _ in Void() }

And in the case that you only want to emit a value if the boolean value is true:

let voidObservable = boolObservable.filter { $0 }.map { _ in Void() }

José
  • 3,112
  • 1
  • 29
  • 42
1

Adding this extension:

public extension ObservableType {
    func mapToVoid() -> Observable<Void> {
        return map { _ in }
    }
}

so you could do

myObservable.mapToVoid()
93sauu
  • 3,770
  • 3
  • 27
  • 43