13

I'm trying to create asimple framework for testing with swift.

I had post the question in objective-c with "create the framework" before. It was resolved.

But I'm trying to create framework with swift.

I encounter the problem. I can't import the MyUtility file in header file.

like below:

enter image description here enter image description here

Have anyone know how to import the file in my custom framework?

thank you very much.

============== edit ===========

for @Jerome L

enter image description here

Community
  • 1
  • 1
dickfala
  • 3,246
  • 3
  • 31
  • 52

2 Answers2

10

In order to import Swift in objective C, you need to use the following syntax:

#import <ProductName/ProductModuleName-Swift.h> 

So I guess in your case ti would be something like:

#import <MyFramewordSwift/MyUtility-Swift.h> 

You can find more details here

Jérôme
  • 8,016
  • 4
  • 29
  • 35
  • Thank you @Jerome L, But I change to #import , It is show file not found.@@ – dickfala Dec 26 '14 at 14:47
  • According to apple documentation you should first: "Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.". Sorry, I don't have xcode right now to test. You can still follow the link I putin my answer. It will explain in detail how to do the import. Thanks – Jérôme Dec 26 '14 at 21:19
  • I had set defines module to yes, I had post the build settings in my article edit part, you can refer the photo. I don't know where the problem in my project or code. Thank you very much. – dickfala Dec 27 '14 at 02:59
  • 1
    this is different case, what you are talking about is importing a swift file into an objective-c file, and that relies on taking the module that the swift file is in and importing that into the .m file. The question here is how to import a swift file into a framework header – bolnad Apr 07 '15 at 19:25
  • The import format is the correct answer. I wish I could give you a million upvotes. – Stepan Hruda Aug 31 '15 at 20:08
5

I ran into the same issue. I have an all swift project and am making a pure swift framework. In order to get the target in the same project file to see the classes in my framework, I needed to explicitly add the modifier public in front of my class declaration and in front of any method that I wanted to be accessible. Here is an example of one of the classes.

private let _sharedInstance = SomeManager()

public class SomeManager: NSObject {
    
    //MARK: - Singleton instance
    
    public class var sharedManager : SomeManager {
        return _sharedInstance
    }

     public func outwardFacingMethod()-> Void {
        internalMethod()
    }
    
    private func internalMethod()-> Void {

    }
   
}

The documentation in this link states here but is a bit buried. If you scroll down to the section called Importing Code from Within the Same Framework Target it says

Because the generated header for a framework target is part of the framework’s public interface, only declarations marked with the public modifier appears in the generated header for a framework target.

mzdogan
  • 91
  • 1
  • 12
bolnad
  • 4,533
  • 3
  • 29
  • 41