Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicitly import specific Swift module

Tags:

swift

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.

like image 313
fluidsonic Avatar asked Oct 16 '14 15:10

fluidsonic


People also ask

What is _exported in Swift?

The @_exported attribute starts with an underscore. That means it's a private Swift attribute. Not a feature, an implementation detail. In short, this attribute lets you export a symbol from another module as if it were from your module.

Does import order matter in Swift?

Import order does not matter. If a module relies on other modules, it needs to import them itself.

How do I import a Swift package into Objective-C?

Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .


2 Answers

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.

like image 148
fluidsonic Avatar answered Oct 15 '22 00:10

fluidsonic


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.

like image 20
mattt Avatar answered Oct 14 '22 22:10

mattt