Skip to content
This repository was archived by the owner on May 10, 2025. It is now read-only.

Combine Compatible Package

ddddxxx edited this page Oct 12, 2019 · 17 revisions

For Package User

Some package allows you to chooes underlying Combine implementation (Combine or CombineX).

For non-Apple-platform, CombineX is the only choice, A Combine Compatible Package should automatically select CombineX implementation.

For Apple platform, Combine is preferred, you can set env variable SWIFT_PACKAGE_USE_COMBINEX to use CombineX.

$ export SWIFT_PACKAGE_USE_COMBINEX="true"

# for command line instruction, execute with env variable
$ swift ...
$ xcodebuild ...

# for Xcode GUI, reopen Xcode with env variable
$ killall Xcode
$ open Package.swift

For Package Creator

To create a Combine Compatible Package, you read env variable and config your package accordingly.

CXCompatible does the hard work for you. you can easily write underlayer-unrelated code.

// in your Package.swift
let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/cx-org/CXCompatible", .branch("master")),
    ],
    targets: [
        .target(name: "MyAwesomePackage", dependencies: ["CXShim"]),
    ]
)
// in your code
import Foundation
import CXShim // Automatically choose `Combine` or `CombineX`

// Avoid name conflict between Foundation.Published and CXShim.Published
typealias Published = CXShim.Published

// Have Fun

Or you can do it yourself.

// in your Package.swift
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Foundation
let useCombineX = ProcessInfo.processInfo.environment["SWIFT_PACKAGE_USE_COMBINEX"] != nil
#else
let useCombineX = true
#endif

if useCombineX {
    // Your package config with CombineX
    // you should define a compilation condition `USE_COMBINEX` here
    // let combineSwiftSetting = [SwiftSetting.define("USE_COMBINEX")]
} else {
    // Your package config with Combine
}
// in your code
#if USE_COMBINEX
import CombineX
#else
import Combine
#endif
Clone this wiki locally