Skip to content

chorim/CombineInterception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CombineInterception

WARNING

If that PR is merged, it will add functionality to CombineCocoa.

Please use CombineCocoa in future.

CombineCocoa supports Swift Package Manager (SPM)

CombineInterception is a library that provides functionality similar to the methodInvoked function from RxCocoa, allowing you to subscribe to the invocation of Objective-C methods.

Reference from https://github.com/EduDo-Inc/CombineCocoa

Usage

TL;DR

import UIKit
import Combine
import CombineInterception

extension UIViewController {
    var viewDidLoadPublisher: AnyPublisher<Void, Never> {
        let selector = #selector(UIViewController.viewDidLoad)
        return publisher(for: selector)
            .map { _ in () }
            .eraseToAnyPublisher()
    }
    
    var viewWillAppearPublisher: AnyPublisher<Bool, Never> {
        let selector = #selector(UIViewController.viewWillAppear(_:))
        return intercept(selector)
            .map { $0[0] as? Bool ?? false }
            .eraseToAnyPublisher()
    }
}

class ViewController: UIViewController {
    private var subscriptions = Set<AnyCancellable>()
    
    private func bind() {
        viewDidLoadPublisher
            .sink(receiveValue: { _ in
                print("viewDidLoad")
            })
            .store(in: &subscriptions)
        
        viewWillAppearPublisher
            .sink(receiveValue: { isAnimated in
                print("viewWillAppearPublisher", isAnimated)
            })
            .store(in: &subscriptions)
    }
}

Dependencies

This project has the following dependencies:

  • Combine.framework

Installation

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/chorim/CombineInterception.git", from: "0.1.0")

Acknowledgments

License

MIT, See the LICENSE file.

The Combine framework are property of Apple Inc.