11

I use a third party Swift framework in my Swift app very often and would like to use it without having to write import ModuleName in every single Swift file over and over again.

Is there a way to specify default imports like it was possible in Objective-C using a .pch file?

I already checked Xcode build settings and swiftc flags but none of them offers this functionality.

fluidsonic
  • 4,655
  • 2
  • 24
  • 34

2 Answers2

37

Actually there's a very easy workaround I could have thought about earlier…

Simply add the following to your app project's Objective-C bridging header:

@import ModuleName;

Swift will pick it up too! No need for import ModuleName in every Swift file where you intend to use the module.

fluidsonic
  • 4,655
  • 2
  • 24
  • 34
  • 2
    won't work if the target being created is a Swift framework dependent on other modules as bridging headers are not permitted – Max MacLeod Feb 19 '16 at 12:02
  • 3
    @MaxMacLeod yep this only works for app targets. For frameworks there doesn't seem to be any functionality or workaround to achieve the same result. But I got used to it :) – fluidsonic Feb 19 '16 at 13:42
4

No, there is no Swift analogue for Objective-C .pch files.

One could ostensibly reduce the number of import statements by creating an umbrella target framework that includes a number of other modules, but there's no way to avoid explicit imports of dependent modules outright.

mattt
  • 19,544
  • 7
  • 73
  • 84
  • 2
    Thanks for the answer mattt. I guess you're correct that there is no standard way to do that in Swift. But I found a way to achieve the same result by just using `@import` in ObjC's bridging header! – fluidsonic Oct 17 '14 at 13:52
  • Swift has `.swiftmodule` files which are importable when one provides its dirname as the value fo the `-I` argument of `swiftc`. [SWM (Swift Modules)](http://github.com/jankuca/swm) can help a lot with these. – J. K. Dec 13 '14 at 11:10