I have a framework with Objective-C and Swift mixed together. It compiles alone but when I import it in the Objective-C iOS app, Swift classes are not found but Objective-C classes are found.
My Swift classes are found inside the framework when importing MyFramework-Swift.h
The iOS app and framework are two different projects in the same workspace.
Defines Module and Embedded content contains Swift Code are set to Yes for all targets and my Swift classes are public with @objc
I tried to use @import MyFramework and #import <MyFramework/MyFramework-Swift.h> with no success.
I don't see the MyFramework-Swift.h header file in the framework's Headers directory into the projects. Not sure if this is normal. It is generated in DerivedData
EDIT: I managed to reproduce the problem with a very simple workspace in Xcode 8 (but probably the same in 7.3):
TestFramework in objective-C Create a A.h file
@class B;
@interface A : NSObject
-(void)print:(B*)caller;
@end
Create a A.m file
#import <Foundation/Foundation.h>
#import "TestFramework-Swift.h"
#import "A.h"
@implementation A
-(void)print:(B*)caller {
[caller test];
}
Create a B.swift file
import Foundation
@objc public class B : NSObject {
public func test() {
print("test");
}
}
Set A.h as a public header
A.h into TestFramework.hHere the TestFramework-Swift.h is not found
Install Objective-C compatibility header to NoNow it compiles !
Update the ViewController.m like this
#import "ViewController.h"
@import TestFramework;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
B* b = [[B alloc] init];
A* a = [[A alloc] init];
[a print: b];
}
[...]
@end
You should have an error on B but not A. I also set Defines Module to Yes in both project without success
Ok I finally found the answer. Install Objective-C compatibility header must be set to Yes and the import must be #import <TestFramework/TestFramework-Swift.h>and not #import "TestFramework-Swift.h" inside the framework
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With