Import Framework in Swift Project, Xcode Import Framework in Swift Project, Xcode xcode 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.


On Swift:

Create Framework:-

  1. Start Xcode -> Create a new Xcode Project -> iOS -> Framework & Library -> Cocoa Touch Framework -> Name the framework(ex. sampleCocoaFramework) -> Create.

  2. Set Target -> General -> Deployment info -> Deployment Target.

  3. Add a public class: File -> New File -> iOS -> Swift File -> Name it (ex. openCocoaClass) -> Create.

  4. Now add some code to the openCocoaClass.swift.

    import Foundationpublic class openCocoaClass {    public init() {    }    public var samplePublicVariable = "samplePublicVariable @ openCocoaClass"    public func samplePublicFunction()    {        print("samplePublicFunction @ openCocoaClass")    }          

    }

  5. Clean the project : Product -> Clean

  6. Configure the scheme settings : Product -> Scheme -> Edit Scheme -> Run -> Build Configuration -> Release.

  7. Build the framework : Product -> Build.

  8. Export the framework : Products -> Select the framework -> Identity and type -> Full Path -> Released Framework.

Adding Framework to Project:-

  1. Start a Xcode project and name it (ex. CocoaFrameworkTest).

  2. Drag and drop the sampleCocoaFramework.framework to the CocoaFrameworkTest's project folder.

  3. Target -> General -> Embed Binaries -> Add Other -> Select Framework -> Copy Items if needed -> Done.

Accessing Framework on ViewController:-

import UIKitimport sampleCocoaFrameworkclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        let frameworkObject =  openCocoaClass.init()        frameworkObject.samplePublicFunction()        print(frameworkObject.samplePublicVariable)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()    }}


According to the Swift documentation

To import Objective-C code into Swift from the same target

  1. In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. For example:

    #import "XYZCustomCell.h"

    #import "XYZCustomView.h"

    #import "XYZCustomViewController.h"

  2. Under Build Settings, make sure the Objective-C Bridging Header build setting under Swift Compiler - Code Generation has a path to the header. The path must be directly to the file itself, not the directory that it’s in. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.

Any public Objective-C headers listed in this bridging header file will be visible to Swift. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

let myCell = XYZCustomCell()myCell.subtitle = "A custom cell"

Also, make sure the "Defines Module" build setting under "Packaging" is set to "Yes."