How can I import Swift code to Objective-C?

前端 未结 15 2349
南方客
南方客 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.

    0 讨论(0)
  • 2020-11-22 03:21
    #import <TargetName-Swift.h>
    

    you will see when you enter from keyboard #import < and after automaticly Xcode will advice to you.

    0 讨论(0)
  • 2020-11-22 03:21

    Find the .PCH file inside the project. and then add #import "YourProjectName-Swift.h" This will import the class headers. So that you don't have to import into specific file.

    #ifndef __IPHONE_3_0
    #warning "This project uses features only available in iPhone SDK 3.0 and later."
    #endif
    
    
    #ifdef __OBJC__
        #import <Foundation/Foundation.h>
        #import <UIKit/UIKit.h>
        #import "YourProjectName-Swift.h"
    #endif
    
    0 讨论(0)
  • 2020-11-22 03:22

    Importing Swift file inside Objective-c can cause this error, if it doesn't import properly.

    NOTE: You don't have to import Swift files externally, you just have to import one file which takes care of swift files.

    When you Created/Copied Swift file inside Objective-C project. It would've created a bridging header automatically.

    Check Objective-C Generated Interface Header Name at Targets -> Build Settings.

    Based on above, I will import KJExpandable-Swift.h as it is.

    Your's will be TargetName-Swift.h, Where TargetName differs based on your project name or another target your might have added and running on it.

    As below my target is KJExpandable, so it's KJExpandable-Swift.h

    0 讨论(0)
  • 2020-11-22 03:24

    If you're using Cocoapods and trying to use a Swift pod in an ObjC project you can simply do the following:

    @import <FrameworkName>;

    0 讨论(0)
  • 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"

    0 讨论(0)
提交回复
热议问题