Import Framework in Swift Project, Xcode

Import Framework in Swift Project, Xcode

If I get you correctly you don't have a separate build target for your framework (you already built it with Xcode 5) and included the framework into your project's build target.

The part of the documentation you're referring to is about frameworks within different targets.
Since your framework is in the project's target this part of the documentation doesn't apply here.

In your case you can't do an import of the framework in your Swift file. That's why you get the error message "No such module myFramework".
myFramework is no module -- it is part of your project's module (which is by default determined by your product name). As such the classes in your framework should be accessible.

However your framework is written in Objective-C. So what you have to do is to import the Swift facing classes in your bridging-header as described here.

Please note that this has nothing to do with the Swift import of a module. The import directive in the bridging-header file just notifies the compiler to 'translate' Objective-C header files to Swift syntax and makes the public header visible to Swift.

So what should you do now?

  • First import the header files you're interested in in the bridging-header. You only need to import the headers you will interact with in Swift.

  • Try to compile your project in this stage. If Xcode can't find the header files of the framework your problem is probably not related to Swift or Xcode 6 but a problem with including frameworks in general.

  • After that try to instantiate a class you imported in the bridging-header, maybe in your AppDelegate.swift. Xcode auto-completion should offer you the type names.

Hope this helps.

How to import framework into XCode project?

That's not a Swift module, so first, you need to create a bridging header, in which you import <BRLMPrinterKit/BRLMPrinterKit.h>, then you can use the BRLM classes in your Swift files.

Using a Objective C framework inside a Xcode Swift Project

The most easy way without digging to deep into settings is..

  1. In your Swift project create one Objective-C Class (.m+.h) file, the naming is not important. This file will be needed anyway so you can code more in objective-c for your project.

  2. When you are asked to generate Bridging Header, say yes.

  3. In the generated Projectname-Bridging-Header.h (not your own created file)
    place your #import <SDF/SDF.h> rule.

  4. Compile once.

  5. Start programming in swift with your ObjC stuff.

Alternative: go into your target settings or project settings and search for "bridging" and change the parameters as you need.
There is (A) one way bridging to swift and (B) one way auto generated bridging into objective-c. Both name conventions can be edited but only one Projectname-Bridging-Header.h will be visible in your project file tree. The other one Projectname-Swift.h is repeatedly generated from Xcode for you. If you try to expose functions from Swift to Objective-C, this "invisible" header file needs to be imported as well, but in the Objective-C file you need it via #import "Projectname-Swift.h".

Xcode import local Swift Package and build from the app

The solution is to create a local Swift Package in the app project and to import the Shared Package there. Then it will be available in the whole project.



Related Topics



Leave a reply



Submit