Using C code in Swift via Objective-C in iOS


Bjarte's Blog

Creating the iOS Application

Create a new project which has Swift as the language.

Add a simple label and an outlet in the viewcontroller for the storyboard.

Creating the Framework

Click on your project and in the list of Targets select the + button to add a new target. Create a new target as a IOS -> Framework & Library -> Cocoa Touch Framework:

Screen Shot 2015-02-18 at 15.52.34

Set the Language to Objective-C:

Screen Shot 2015-02-18 at 15.53.45

Creating the C Code

Create a new file and select C File as the template.

Screen Shot 2015-02-18 at 15.56.10

Write a simple method and expose that in the simple.h header file:

Screen Shot 2015-02-18 at 15.47.21Screen Shot 2015-02-18 at 15.47.29

Creating the Objective-C Bridge for Swift

Create a new file with Cocoa Class template and select NSObject as subclass. In the file add a method to get message:

Screen Shot 2015-02-18 at 15.48.06

Expose this class in the header file with OBJC_EXPORT:

Screen Shot 2015-02-18 at 15.47.47

This will allow us to use this from swift.

Also we need to open the inspector and…

View original post 203 more words