How can I import Swift code to Objective-C?

前端 未结 15 2723
南方客
南方客 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:28

    Here's what to do:

    1. Create a new Project in Objective-C

    2. Create a new .swift file  

      • A popup window will appear and ask "Would You like to configure an Objective-C bridging Header".
      • Choose Yes.
    3. Click on your Xcode Project file

    4. Click on Build Settings

    5. Find the Search bar and search for Defines Module.

    6. Change value to Yes.

    7. Search Product Module Name.

    8. Change the value to the name of your project.

    9. In App delegate, add the following : #import "YourProjectName-Swift.h"


    Note: Whenever you want to use your Swift file you must be import following line :

    #import "YourProjectName-Swift.h"

提交回复
热议问题