Shortcut for multi-target IMPORT

Is there a way to create a "macro" or something so I don't have to have this code in EVERY file of a multi-target project? somehow I can predefine it once, and use a single line shortcut everywhere?

#if !os(macOS)
import UIKit
import Cocoa
import Foundation

One solution would be to create a "shim" framework that re-exports UIKit or Cocoa. The framework OpenCombine uses this technique and creates a module with a single Swift file that reads:

#if canImport(Combine)
@_exported import Combine
@_exported import OpenCombine
#if canImport(OpenCombineDispatch)
@_exported import OpenCombineDispatch
#if canImport(OpenCombineFoundation)
@_exported import OpenCombineFoundation

The @_exported import reimports all of the symbols but it is not part of Swift's public API so it could potentially break in a future release.

Terms of Service

Privacy Policy

Cookie Policy