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
#else
import Cocoa
#endif
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
#else
@_exported import OpenCombine
#if canImport(OpenCombineDispatch)
@_exported import OpenCombineDispatch
#endif
#if canImport(OpenCombineFoundation)
@_exported import OpenCombineFoundation
#endif
#endif

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