Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Framework in Swift Project, Xcode

I'm trying to import myFramework into a project. I've added myFramework in Build Phases->Link Binary With Libraries.

Objective-c works:

#import <UIKit/UIKit.h> #import <myFramework/myFramework.h> 

But with in Swift, I get a No such module myFramework error:

import UIKit import myFramework 

According to the Swift documentation:

Importing External Frameworks

You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to Yes.

You can import a framework into any Swift file within a different target using the following syntax:

SWIFT

import FrameworkName 

You can import a framework into any Objective-C .m file within a different target using the following syntax:

OBJECTIVE-C

@import FrameworkName; 

I created myFramework using Xcode 5. Xcode 5 doesn't have a "Defines Module" build setting.

Where is the problem?

like image 456
phnmnn Avatar asked Jun 05 '14 10:06

phnmnn


People also ask

How do I add a framework to a Swift project?

Select the target for where you want to add frameworks in the project settings editor. 3. Select the “Build Phases” tab, and click the small triangle next to “Link Binary With Libraries” to view all of the frameworks in your application. 4.To Add frameworks, click the “+” below the list of frameworks.


1 Answers

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.

like image 87
mic Avatar answered Sep 28 '22 06:09

mic