Compatible with Objective-C Swift Code

Nikita Lazarev-Zubov
6 min readMar 3, 2019

Despite the fact that Apple have provided us with detailed documentation on how to use Swift code inside Objective-C application (and vice versa), it isn’t felt enough when it comes to it. When I had a need to provide Swift framework with Objective-C compatibility, Apple documentation raised more questions then gave answers (or at least left many gaps). Intensive search proved that the subject is lit quite poorly: a couple of questions on StackOverflow and some introductory articles — that’s all I’d found.

This post is the generalization of the found information and my own experience. All described methods don’t claim to be a really good practice, they just offer a way to solve the problem.

TL;DR. In order to use Swift code inside Objective-C one must scrifice some Swift features and write a wrapper around original Swift code that won’t use non-compatible features (like structs, generics, enum associated values, protocol extensions etc.). All wrapper classes must inherit NSObject.

Beginning

So, we have an Objective-C based project and some Swift code module that is wanted. It could be a Swift framework added by the help of CocoaPods. In this case as usual, we add dependency to Podfile, run pod install, open xcworkspace file.

--

--

Nikita Lazarev-Zubov
Nikita Lazarev-Zubov

Written by Nikita Lazarev-Zubov

Swift and general programming topics, Agile software development, soft skills

No responses yet