Switching from C++ to Swift.

The Algoworks’ image on language performance from “Shift to SWIFT: An Unusual Journey from Objective-C”.

A few months ago I decided to explore Apple’s ecosystem from the developer’s point of view and I was surprised how similar is C++ to Swift is. But before that, I had experience with Embedded Software testing in Automotive projects and in making tutorial projects in C++ using Linux that bolstered me to learn the three inviolate principles of object-oriented programming by heart and made me curious about the internals of macOS and iOS.

Initially, I began discovering the Objective-C programming language and installed its compiler on Linux via the terminal command:

sudo apt-get –y install gobjc gnustep gnustep-devel

From the first look, this language has similarities with the C language and contains some extensions to it like OOP methods and classes. I thought that would be a great idea to try it before catching the recondite design paradigms of C++. Overall, it worked out! I stopped hesitating on job interviews and finally — felt confident about sacrosanct C++ philosophy. Besides, I got the pith of Standard Template Library which contained data structures with algorithms on its maintenance.

By the way, my second step in researching iOS was my draft application on my iPhone where I tried to bind the Objective-C code to C++. Surprisingly, I have found the bridging solution which I tried on my example of a blank Twitter application from “Head First iPhone and IPad Development: A Learner’s Guide to Creating Objective-C Applications for the iPhone and IPad”. Here is the solution which I have found on GitHub.

  1. Create the definition of a pointer to the class which is written in C++ in your ViewController header.
#import <UIKit/UIKit.h>

2. Outline its body in your ViewController implementation and place it after the headers import section. Do not forget to include the C++ header which contains your class.

#import "InstaTwitViewController.h"
#import "somecppfile.hpp"

3. Describe the class, its properties, bodies, and methods.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

4. Finally, create an instance of the object and call this method in your ViewController implementation section. Also, rename your *.m files to *.mm.

@implementation InstaTwitViewController

5. To prevent memory leaks — deallocate your C++ object from the heap.

- (void)dealloc

For a moment, I had a belief that — writing C++ applications for iOS is an excellent idea! But here are a few moments that prevented me to do this:

  • The complexity of the process.
  • Swift is a way simpler solution for building apps than Objective-C++.

Additionally, Swift is Apple’s replacement for the old Objective-C syntax which is recommended nowadays. There are myriad external libraries and Frameworks in Cocoapods written in Swift comparably to Objective-C. Let us take a look at similar syntax but in Swift:

  1. Model file.
import Foundation

2. Controller file with connected IBAction button action.

import UIKit

Is not that easy as it seems? In my view, it is! Conspicuously, There are two steps instead of five. In the end, I am happy going with Swift than with Objective-C++ and sticking to Apple’s technologies!

Ultimately, if you have some experience with C/C++ or with Embedded systems and want to switch to mobile applications development, I advise you to go with iOS. Swift is the intelligible language for beginners and some pre-intermediate coders with little experience.

Happy coding!

Embedded iOS Developer. In den Vollbeschäftigung.🇨🇭https://github.com/alimovlex