Using Third-Party Objective-C Frameworks in Swift Project

Peter Wood in Swift
04 Jul 2014, 14:54

Hello again.

This is our first post out of series dedicated to tips and tricks for Apple’s new programming language, Swift. Our workarounds will be mainly for current lacks and bugs in Swift language itself, as well as for some Swift solutions which we find unobvious and clumsy.

Surely, in course of time certain solutions will become out-of-date, as soon as Apple provides fixes and improvements or develops missing features. But today we hope developers using Swift in real projects will find them helpful.

Quite often we need to use third-party frameworks in our projects. Lots of ready-to-use solutions are already written in Objective-C, so there’s no sense to wait for their analogs in Swift.

It’s really great that Swift permits to use Objective-C code.

Today we’ll see how to do this.

1. Add bridging header:

Choose Header file.

2. Specify the file name. For example, Swift-Bridging-Header.h

3. Set the file name in target settings in “Objective-C Bridging Header”:
swift bridging header

4. Import the required framework into the file:
#import <FlashInApp/FlashInApp.h>

That’s it! Now we can use Objective-C class of this framework in our Swift project:
var rect = NSMakeRect(0, 0, 100, 100)
var view = ESFlashView(frame: rect)

PS This can be done via Xcode as well. Just add Objective-C class into the project. Then delete it. Xcode will create bridging header and specify it in the target settings.

Stay tuned,
Peter Wood from Eltima Software.