Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and import swift framework

I'm new in swift programming. I need to create pure swift framework and import it in my existing pure swift project. When I try to import swift framework, I'm getting this error message:

"Could not build Objective-C module '<myModule>'"

Framework File Structure

Test.h

 import Foundation

public class Test {
    class func printTest() {
        println("111");
    }
}

Asdf.h

import UIKit
public class Asdf: Test {
    class func echo() {
        println(888);
    }
}

myModule-Swift.h

#ifndef <myModule>_<myModule>_Swift_h
#define <myModule>_<myModule>_Swift_h

#endif

After framework build, i have added framework in my existing project and get this Project Structure and import problem screen

What am I doing wrong? Thanks For Help!

@findall comment answer - I tried to add all framework files to my project root folder and inside project folder, but again got the same error Add Framework files to project root

Add Framework files inside project folder

like image 874
styopdev Avatar asked Oct 20 '14 08:10

styopdev


People also ask

How do I create a custom framework in Swift?

In the app, select the project from the project navigator, select the Stocktance target, and scroll to Frameworks, Libraries, and Embedded Content. Click on the plus button, click Add Other… and select Add Files… Navigate to the SettingsKit folder and select it. We've added the framework to the project.

What is framework in Swift?

In Swift parlance, a module is a compiled group of code distributed together. A framework is one type of module while an app is another. Note: If you want to learn more about frameworks, read What are Frameworks?.


2 Answers

I've done with the following steps.

  1. Create a framework project, for example named "FooKit". (Cocoa Touch Framework to be selected)
  2. Create a ".swift" file and add a public symbol, for example public func foo(), to it.
  3. Create an use-side (app) project. (I've chosen Single View Application)
  4. Open the root directory of "FooKit" by Finder, drag "FooKit.xcodeproj" there and drop it into the app project via Project Navigator.
  5. Add "FooKit" to Target Dependencies in the app's Build Phases setting.
  6. Add "FooKit.framework" to Embedded Binaries in the app's General setting.

Now you can build like this code in the use-side app.

import FooKit

func bar() {
    foo()
}
like image 52
findall Avatar answered Oct 08 '22 20:10

findall


In Xcode 7.2 using Swift 2.1 I managed to get past the mentioned error by making sure that the build settings of your pure Swift framework called Foo are as follows:

1) Build Settings->Packaging->Defines Module = Yes

2) Build Settings->Swift Compiler - Code Generation->Install Objective-C Compatibility Header = Yes (if you do not need to import your Swift framework into Objective C set this setting to No)

3) Erase the string value of Build Settings->Swift Compiler - Code Generation->Objective-C Bridging Header

4)Build Settings->Swift Compiler - Code Generation->Objective-C Generated Interface Header Name = Foo-Swift.h (if you do not need to import your Swift framework into Objective C erase this setting as you did in step 3))

5) Make sure that Build Settings->Packaging->Product Name = Foo

6) Add to the public umbrella header of your framework (Foo.h), which you can use to import your Swift code to Objective C, the following line:

#import "Foo-Swift.h"

(but if you do not need to import your Swift code into objective C skip this step)

6) Add the following line to the Swift file where you want to use the module Foo:

import Foo

Important notes:

1) Ensure that your umbrella header Foo.h is set to public in the File Inspector, otherwise this won't work.

2) If you are using your pure Swift framework in Objective C files, ensure that:

  • the Foo-Swift.h header has generated the correct interface by going to Foo.h and then clicking on the top left corner menu of the Xcode code editor and then choosing Includes->Foo-Swift.h (I had to rebuild the Foo framework a few times until Xcode caught-up with the change and then generated the correct interface in Objective C, so you may need to do that as well)

  • your swift classes inherit from NSObject or they won't be available to Objective C code

like image 44
jvarela Avatar answered Oct 08 '22 20:10

jvarela