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
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:
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
Build and Run.
#import <TargetName-Swift.h>
you will see when you enter from keyboard #import < and after automaticly Xcode will advice to you.
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
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
If you're using Cocoapods and trying to use a Swift pod in an ObjC project you can simply do the following:
@import <FrameworkName>;
Here's what to do:
Create a new Project in Objective-C
Create a new .swift
file
Click on your Xcode Project file
Click on Build Settings
Find the Search bar and search for Defines Module.
Change value to Yes.
Search Product Module Name.
Change the value to the name of your project.
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"