How to import a Swift framework globally?

IosSwift

Ios Problem Overview


I want to have a way to import my Swift Cocoapods globally in every class, how can I achieve this?

I tried a lot of things and they didn't work. Here are some ways I haven't tried and thought may be possible if found a way to work them:

Have a general import statement like UIKit and put everything in there. (Edit: This failed)

Somehow put Swift frameworks in the Obj-C briding header and import the stuff in there.

Ios Solutions


Solution 1 - Ios

You should be able to import it globally by adding @_exported before the import.

@_exported import Podname

However, like the previous posters mentioned, this is not recommended.

Solution 2 - Ios

It's strongly discouraged in Swift because that would introduce implicit coupling between modules.

However, you can make a certain symbol available globally by declaring a typealias in the module that imports the other module:

import ModuleName
public typealias ClassName = ModuleName.ClassName

Solution 3 - Ios

As of Swift4:

  • Being in a Swift project
  • Want to have another Swift project globally imported (and using cocoapods)

I just managed to do that by adding the following line to my bridging header:

#import <PodName/PodName-Swift.h>

How good/bad this practise is? Not sure, but I just wanted some extensions globally available in my project. this did the trick.

Solution 4 - Ios

There is no way to do this. And this is not a bug, this is a language feature (so far, as speaking of Swift 2.2).

Swift uses modules (Apple introduced them in Xcode 5 for Objective-C) and each file is an semantic unit, so you need to explicitly inform Xcode which modules are exposed to defined file.

Not only there is no support for your described behaviour, but you shouldn't also try to bypass it. Using unnecessary (unused) modules could theoretically produce slower code (taking into account that compiler uses this information to its optimisation process).

Solution 5 - Ios

You can manually achieve the same functionality by:

  • Creating the Objective-C Bridging Header file;
  • Adding #import <UIKit/UIKit.h> to the Objective-C Bridging Header file (so that you don't need to repeat this for every single .swift file).

for pods you have to do like #import <SwiftyJSON/SwiftyJSON-umbrella.h>

Solution 6 - Ios

A reason you wouldn't want to do this:

Imagine if both your frameworks would use the same method name, it would make things ambiguous for the compiler.The compiler won't know which method it should run.

To find out more see this question

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEsqarrouthView Question on Stackoverflow
Solution 1 - IosChristoffer WinterkvistView Answer on Stackoverflow
Solution 2 - IosIgor MakarovView Answer on Stackoverflow
Solution 3 - IosaceciliaView Answer on Stackoverflow
Solution 4 - IosKrodakView Answer on Stackoverflow
Solution 5 - IosEslam MoemenView Answer on Stackoverflow
Solution 6 - IosmfaaniView Answer on Stackoverflow