How can I import Swift code to Objective-C?

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

    If you want to use Swift file into Objective-C class, so from Xcode 8 onwards you can follow below steps:

    If you have created the project in Objective-C:

    1. Create new Swift file
    2. Xcode will automatically prompt for Bridge-Header file
    3. Generate it
    4. Import "ProjectName-Swift.h" in your Objective-C controller (import in implementation not in interface) (if your project has space in between name so use underscore "Project_Name-Swift.h")
    5. You will be able to access your Objective-C class in Swift.

    Compile it and if it will generate linker error like: compiled with newer version of Swift language (3.0) than previous files (2.0) for architecture x86_64 or armv 7

    Make one more change in your

    1. Xcode -> Project -> Target -> Build Settings -> Use Legacy Swift Language Version -> Yes

    Build and Run.

提交回复
热议问题