How can I import Swift code to Objective-C?

前端 未结 15 2734
南方客
南方客 2020-11-22 02:52

I have written a library in Swift and I wasn\'t able to import it to my current project, written in Objective-C.

Are there any ways to import it?

#i         


        
15条回答
  •  佛祖请我去吃肉
    2020-11-22 03:17

    First Step:-

    Select Project Target -> Build Setting -> Search('Define') -> Define Module update value No to Yes

    "Defines Module": YES.

    "Always Embed Swift Standard Libraries" : YES.

    "Install Objective-C Compatibility Header" : YES.

    Second Step:-

    Add Swift file Class in Objective C ".h" File as below

    #import 
    
    @class TestViewController(Swift File);
    
    @interface TestViewController(Objective C File) : UIViewController
    
    @end
    

    Import 'ProjectName(Your Project Name)-Swift.h' in Objective C ".m" file

    //TestViewController.m 
    #import "TestViewController.h"
    
    /*import ProjectName-Swift.h file to access Swift file here*/
    
    #import "ProjectName-Swift.h"
    

提交回复
热议问题