How do I create a dummy or empty Publisher?

I'm having a bit of trouble getting my head around publishers.

My current project is targetting macOS and iOS. I found a publisher-based way to handle keyboard height changes that works great in iOS.

Now I just need to find a way to create an empty/dummy publisher for macOS.

(I can solve this problem other ways, but am feeling embarrassed that I can't figure out how to make this empty publisher.)

Any/all guidance appreciated.


the code snippet below returns the following error at line 16: Unable to infer closure type in the current context


thanks,


extension Publishers {
   
#if os(iOS)

    static var keyboardHeight: AnyPublisher<cgfloat, never=""> {
        let willShow = NotificationCenter.default.publisher(for: UIApplication.keyboardWillShowNotification)
            .map { return $0.keyboardHeight }

        let willHide = NotificationCenter.default.publisher(for: UIApplication.keyboardWillHideNotification)
            .map { _ in CGFloat(0) }

        return MergeMany(willShow, willHide)
            .eraseToAnyPublisher()
    }
#elseif os(macOS)
    static var keyboardHeight = AnyPublisher<cgfloat, never=""> {
        return Empty(completeImmediately: false)
    }
#endif
}

Accepted Reply

Seems your code is modified when posting, this site has a bug in code editing feature and you should better check it.


You may be confused in some syntax of defining static property.


If you want to make your `keyboardHeight` as a computed property, you may need to write it as follows:

    static var keyboardHeight: AnyPublisher<CGFloat, Never> {  //<- not Equals sign `=`, but Colon `:`
        return Empty(completeImmediately: false).eraseToAnyPublisher()
    }


If you prefer stored propety, it should be:

    static let keyboardHeight: AnyPublisher<CGFloat, Never> = Empty(completeImmediately: false).eraseToAnyPublisher()

Replies

Seems your code is modified when posting, this site has a bug in code editing feature and you should better check it.


You may be confused in some syntax of defining static property.


If you want to make your `keyboardHeight` as a computed property, you may need to write it as follows:

    static var keyboardHeight: AnyPublisher<CGFloat, Never> {  //<- not Equals sign `=`, but Colon `:`
        return Empty(completeImmediately: false).eraseToAnyPublisher()
    }


If you prefer stored propety, it should be:

    static let keyboardHeight: AnyPublisher<CGFloat, Never> = Empty(completeImmediately: false).eraseToAnyPublisher()

thank you :-)