Sitemap

Swift Code, Compatible with Objective-C

5 min readMar 3, 2019
Press enter or click to view image in full size

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 doesn’t fell enough when it actually comes to it. When I had to provide a Swift framework with Objective-C compatibility, Apple documentation raised more questions than 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 summary of the information I found and my own experience. All described methods don’t claim to be good practices, they just offer a way to solve the problem.

Not a member? Read here for free.

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 from NSObject.

The Beginning

So, we have an Objective-C-based project and a Swift module. To make it visible from Objective-C code there’s no need in using import of entire module (as we’re used to do in Swift), nor of single files (as we’re used to do in Objective-C)…

--

--

Nikita Lazarev-Zubov
Nikita Lazarev-Zubov

Written by Nikita Lazarev-Zubov

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

No responses yet