5

When you add a Swift file to an Objective-C project, Xcode will generate a Swift-to-ObjC header, as described here: http://ericasadun.com/2014/08/21/swift-calling-swift-functions-from-objective-c/

Without this header it is not possible to call Swift code from Objc-C. However Xcode is not auto-generating this header for my framework target.

If I create an Objective-C app and drop a Swift file into it, then it does auto-generate one, so I suspect it's because I'm building a framework and not an app. Without one its not possible to use the Swift code from the Obj-C code.

I tried using the one which was generated for the app (after renaming it and putting it in the appropriate DerivedData folder ) but Xcode didn't update it and actually it will eventually delete it, so manually creating or trying to maintain this file is not feasible.

How can I make Xcode generate this header for a framework target, so that I can call my Swift code from my Obj-C code?

And remember folks: the question is about calling Swift from Obj-C not calling Obj-C from Swift.

7
  • What do you need? A bridging header or a generated Obj-C interface? Commented Apr 14, 2016 at 3:08
  • I need to be able to add Swift source files to an Obj-C framework and to able to invoke the Swift classes from the Obj-C code. Commented Apr 14, 2016 at 3:13
  • And what problem are you having? Commented Apr 14, 2016 at 3:13
  • There is no Swift bridging header generated. Without a Swift bridging header it is not possible to call Swift code from Obj-C code within the same code base. Commented Apr 14, 2016 at 3:15
  • 1
    Interesting, I just set up a test project and there seems to be a catch-22. Xcode wont generate the "FrameworkName-Swift.h" file unless I specify a bridging header in the Swift compiler build settings, but if I do that then I get an error that "using bridging headers with framework targets is unsupported" Commented Apr 14, 2016 at 3:35

2 Answers 2

12

I created a new Framework project, added both Obj-C and Swift files, and was able to do this:

// MyObjCClass.m

#import "MyObjCClass.h"
#import <MyFramework/MyFramework-Swift.h>

@implementation MyObjCClass
- (void)test {
    [[MySwiftClass alloc] init];
}
@end

Note that your Swift class must be public:

public class MySwiftClass: NSObject {
    // ...
}

More information is available in Apple's Swift/Obj-C interop documentation under "Importing Swift into Objective-C".

Sign up to request clarification or add additional context in comments.

13 Comments

It seems that the public modifier is the key.
Note that in the documentation I linked, it says: "[This header] also contains those marked with the internal modifier if your app target has an Objective-C bridging header."
Yeah, I tried internal and it didn't work, because you can't have an Objective-C bridging header on a framework
Before posting the question I also created a new framework and found I could not do likewise with MyFramework-Swift.h as you have done, and my class was public. But the difference is I prefaced the class with @objc and interestingly you haven't. I'll do some more experiments without it and see what happens.
The presence of @objc doesn't affect it in my sample project. As far as I can tell, it's necessary to use <angle brackets> when importing the header.
|
-2

#import <MyFramework/MyFramework-Swift.h>

ATTENTION: this header file is generated auto!!!! Don't create it manually!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.