CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.

CombineActive 0.1.20251002

CombineActive 0.1.20251002

Maintained by lalawue.



CombineActive 0.1.20251002

  • By
  • suchaaa

CombineActive

Introduction

CombineActive is a lightweight reactive extension library based on Apple's Combine framework, designed specifically for UIKit controls. It provides a clean and intuitive API for observing UI events, making your iOS development more modern and functional.

The library aims to simplify the use of the Combine framework with UIKit, offering convenient methods similar to RxSwift/RxCocoa but built on the native Combine framework.

Features

  • Reactive UI Event Observing - Transform common UI control events into Combine Publishers
  • Memory Safety Management - Built-in DisposeBag mechanism for automatic subscription lifecycle management
  • Extensive Control Support - Supports most commonly used UIKit controls
  • Clean API Design - Access all extension features through the .cx namespace
  • Lightweight Implementation - No external dependencies, focusing only on providing essential extension features

Supported Controls

  • UIButton - tap events
  • UITextField - text, attributedText, editing events
  • UITextView - text, attributedText, editing, selection events
  • UISlider - value change events
  • UISwitch - value change events
  • UIBarButtonItem - tap events
  • UIView - gesture events (tap, long press, pan)
  • Generic UIControl event support

Installation

CombineActive is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CombineActive'

Then run:

pod install

Basic Usage

Import the CombineActive framework:

import CombineActive

UIButton Example

import Combine
import CombineActive

class ViewController: UIViewController {
    @IBOutlet weak var button: UIButton!
    private var disposeBag = CxDisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        button.cx.tap
            .sink { _ in
                print("Button tapped!")
            }
            .disposed(by: disposeBag)
    }
}

UITextField Example

textField.cx.text
    .sink { text in
        print("Text changed: \(text ?? "")")
    }
    .disposed(by: textField.cx.disposeBag)

textField.cx.didBegin
    .sink {
        print("Begin editing")
    }
    .disposed(by: textField.cx.disposeBag)

Using DisposeBag for Automatic Subscription Management

button.cx.tap
    .sink { _ in
        print("This subscription will be automatically cancelled when the ViewController is deallocated")
    }
    .disposed(by: cx.disposeBag)

Advanced Features

Timer Extension

// Create a timer that emits every second
Timer.intervalMain(1.0)
    .sink { timeInterval in
        print("Time interval: \(timeInterval)")
    }
    .disposed(by: cx.disposeBag)

Publisher Extensions

// Delay on main queue
publisher.delayMain(1.0)

// Debounce on main queue
publisher.debounceMain(0.5)

// Throttle on main queue
publisher.throttleMain(0.5)

Example Project

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 13.0+
  • Swift 5.0+
  • Xcode 11.0+

Author

lalawue, [email protected]

License

CombineActive is available under the MIT license. See the LICENSE file for more info.