Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift classes not found in iOS app when importing a mixed framework

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):

  • Create a new Cocoa touch framework 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

  • import A.h into TestFramework.h
  • Try to compile the framework

Here the TestFramework-Swift.h is not found

  • Set Install Objective-C compatibility header to No
  • Try to compile the framework

Now it compiles !

  • Create a new iOS Objective-C Single view application App
  • 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

like image 734
Estar Avatar asked Sep 02 '25 10:09

Estar


1 Answers

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

like image 168
Estar Avatar answered Sep 03 '25 22:09

Estar