In the new Swift language from Apple, how does one call Objective-C code?
Apple mentioned that they could co-exist in one application, but does this mean that one could technically re-use old classes made in Objective-C whilst building new classes in Swift?
The Reasoning
Objective-C is a platform-independent language, whereas Swift is platform-dependent. Writing non-platform-dependent code (business logic libraries) in Swift would thus not be wise. However, writing platform-dependent code in it (interface related for example) would be perfectly fine. Not to say it would be a good idea, however it is definitely an interest.
You can read the nice post Swift & Cocoapods. Basically, we need to create a bridging header file and put all Objective-C headers there. And then we need to reference it from our build settings. After that, we can use the Objective-C code.
Also have a look at Apple's document Using Swift with Cocoa and Objective-C as well.
One more thing I would like to add here:
I am very thankful for @Logan's answer. It helps a lot to create a bridge file and setups.
But after doing all these steps I'm still not getting an Objective-C class in Swift.
I used the
cocoapods
library and integrated it into my project. Which ispod "pop"
.So if you are using Objective-C pods in Swift then there may be a chance that you can not able to get or
import
the classes into Swift.The simple thing you have to do is:
<YOUR-PROJECT>-Bridging-Header
file and#import <ObjC_Framework>
to@import ObjC_Framework
For example: (Pop library)
Replace
with
Use
clang import
when#import
is not working.Quote from the documentation:
Click on the New file menu, and chose file select language Objective. At that time it automatically generates a "Objective-C Bridging Header" file that is used to define some class name.
"Objective-C Bridging Header" under "Swift Compiler - Code Generation".
I have added the project on github that include small sample to call objective c code from swift.
Call ObjectiveC class from swift
Using Objective-C Classes in Swift
** If you have an existing class that you'd like to use, perform Step 2 and then skip to Step 5. (For some cases, I had to add an explicit
#import <Foundation/Foundation.h
to an older Objective-C File.) **Step 1: Add Objective-C Implementation -- .m
Add a
.m
file to your class, and name itCustomObject.m
.Step 2: Add Bridging Header
When adding your
.m
file, you'll likely be hit with a prompt that looks like this:Click YES !
If you did not see the prompt, or accidentally deleted your bridging header, add a new
.h
file to your project and name it<#YourProjectName#>-Bridging-Header.h
.In some situations, particularly when working with Objective-C frameworks, you don't add an Objective-C class explicitly and Xcode can't find the linker. In this case, create your
.h
file named as mentioned above, then make sure you link its path in your target's project settings like so:Note
It's best practice to link your project using the
$(SRCROOT)
macro so that if you move your project, or work on it with others using a remote repository, it will still work.$(SRCROOT)
can be thought of as the directory that contains your .xcodeproj file. It might look like this:Step 3: Add Objective-C Header -- .h
Add another
.h
file and name itCustomObject.h
.Step 4: Build your Objective-C Class
In
CustomObject.h
In
CustomObject.m
Step 5: Add Class to Bridging-Header
In
YourProject-Bridging-Header.h
:Step 6: Use your Object
In
SomeSwiftFile.swift
:There is no need to import explicitly; that's what the bridging header is for.
Using Swift Classes in Objective-C
Step 1: Create New Swift Class
Add a
.swift
file to your project, and name itMySwiftObject.swift
.In
MySwiftObject.swift
:Step 2: Import Swift Files to ObjC Class
In
SomeRandomClass.m
:The file:
<#YourProjectName#>-Swift.h
should already be created automatically in your project, even if you can not see it.Step 3: Use your class
Note:
1. CodeCompletion wasn't behaving as accurately as I'd like it to. On my system, running a quick build with "cmd + r" seemed to help Swift find some of the Objective-C code and vice versa.
2. If you add a
.swift
file to an older project and get error:dyld: Library not loaded: @rpath/libswift_stdlib_core.dylib
, try completely restarting Xcode.3. While it was originally possible to use pure Swift classes in Objective-C by using the
@objc
prefix, after Swift 2.0, this is no longer possible. See edit history for original explanation. If this functionality is reenabled in future Swift versions, the answer will be updated accordingly.