In June 2014 in the world of Apple, something happened that no one expected: Apple has introduced a new object-oriented programming language - Swift, which should replace Objective-C, previously used to develop applications for OSX & iOS.
Swift vs. Objective-C
Apple has announced Swift as a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS that completely can replace the Objective-C.
Due to several key features, Swift has the potential to become the only preferred programming language for creating engaging, flexible, consumer-friendly applications.
Before focusing on Swift vs. Objective-C comparison, let us give a brief background to the history of iOS programming languages development.
In the distant 2008 Apple has released the iPhone SDK 2.0. This event caused the revolution in software development and gave the impulse to a new generation of developers. They are now well known as iOS developers. That was a real challenge for many of them to develop apps on Objective-C because they had had no experience with it before.
Later, in 2010 Apple launched version 4 of the iPhone OS (the previous name of iOS). The updated version comprised not only the new features in software but the changes in the Developer Agreement. According to the new usage agreement, applications must be originally written in Objective-C, C, C++ … and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs.
The official reason for such an unexpected bombshell was the Steve Job’s desire to prevent the usage of cross-platform tools such as Flash CS5.
Nevertheless, the changes in apps policy provided by Apple were successful and helped the company populate the App Store with thousands of apps.
But in reality Objective-C’s popularity was a result of the growing app ecosystem, but not vice versa.
In fact, many developers were dissatisfied with Objective-C at that moment, and this has given rise to many attempts to write iPhone apps in other programming languages.
Apple continually worked on Objective-C improvements with each release, adding blocks and literals, simplified memory management with automatic reference counting.
Having worked for 6 years on Objective-C improvements, Apple Corp has to admit the fact that it might not be the best coding language for writing mobile apps. So, it was decided to arrange another challenge for developers by introduction a new iOS coding language called Swift.
Apple announced that Swift code is safe by design and it is interactive and fun to write apps on it. Also, Swift syntax is considered to be easy to learn.
Apple has done a great job and implemented the big changes in the new iOS coding language. These innovations have removed the unsafe pointer management and introduced powerful new features.
At the same time, the interaction with both Objective-C and C is maintained.
Swift provides the great speed that ensures real-time feedback and can be smoothly included into existing Objective-C code.
It means that code written on Swift is able to work with code written in C programming languages or Objective-C within the same project.
But there are two key differences between Objective-C and Swift languages:
- Swift is not a strict superset of the C language. It means that Swift is free to make use of syntax constructs. For instance, this allows it to implement custom operators in Swift.
- Swift is statically typed, not dynamically typed. Swift can take advantage of many of the recent advances in type systems.
The point is clear code
The pundits admit: the main Swift’s advantage is in its code, which is easy to read and write.
The number of code lines needed to implement the option on Swift is a lot fewer than for the same option on Objective-C.
Swift supports inferred types, and that help keep code clean and avoid minor mistakes.
Memory is managed automatically, so there is no need to type semi-colons. Swift also includes some elements borrowed from other languages. For example, named parameters brought forward from Objective-C. These features are realized in a clean syntax that makes APIs in Swift easy to read and support.
One more Swift great feature is that Swift was made an open source in its third release. So, broad community of developers, both inside Apple and hundreds of outside contributors, can work together to make Swift even more magnificent.
Five Reasons for Using Swift
Since Swift second release, The App Solutions developers have been experimenting with it inside our company. Because of the relative youth of Swift, we did our first project using Swift at the end of 2015. It was an app for startup Nuwbii, an amazing resource which helps local businesses promote their services. The App Solutions team developed iOS mobile application for this project based entirely on Swift.
Below we gathered and shared Swift advantages that we've discovered throughout our experience using this new language. There are five reasons why you should use Swift that our developers have spotted while working on the project Nuwbii in particular.
Really groovy stuff when you need to experiment with something, to check out or to try smth. The new advantage in XCode 6 that provides the programmer with space for experiments. It looks like a simple editor window where you can write code. The code is compiled and executed immediately. There is no need to build the project, and then run the emulator in order to test it. Everything runs simultaneously: you write code and instantly see the result.
A special panel on the right is capable of displaying almost anything - from simple lines to the finite control elements, from simple images to the graph of the variable value changes during processing.
2. Swift requires less code
Swift has reduced the amount of code that is needed to implement the repetitive statements and string manipulation.
With Objective-C, there were a lot of problems that caused app crashes.
For instance, the developers were faced with messing up the order or using the wrong string token.
Here, Swift win by providing a code that is now less error prone because of its inline support for manipulating text strings and data.
And finally, classes are not divided into two parts anymore: the interface and the implementation.This halves the number of files in the project, which makes it easier to navigate through it.
3. Swift is faster and safer
Apple continues to improve the speed at which Swift can run app logic. Swift is applying best practices and demonstrates the following results: in an 8.5-fold boost for FFT algorithm performance (compared to C++ with only a 1.1-time performance). Also Swift has outperformed C++ for the Mandelbrot algorithm by a factor of a mere 1.03.
To provide the safety Swift triggers a runtime crash while you use a nil optional variable.
This crash enables the consistent behavior, which makes easier the bug-fixing process because it forces the developer to fix the problem immediately. This gives the possibility to fix the bug sooner or completely avoid it in code.
4. Swift supports dynamic libraries
Swift switched from static to dynamic libraries. Dynamic libraries are executable chunks of code that can be linked to an app. This feature helps current Swift apps connect with the newer versions of the language due to its constant evolution.
5. Swift is unified with memory management
The model remains the same: Automatic Reference Counting (ARC) with strong & weak references. But weak references are now available via Optional values. Plus unowned references were added, which are used when the applying of strong references are unacceptable because of a creation of circular references (which lead to memory leaks).
Now the developers shouldn’t think about memory for every digital object they have created. ARC handles all memory management at compile time and works across both procedural and object-oriented code.
Swift adds language features that make developers' code safer, faster, more reliable, and more predictable compared to Objective-C.
Summarizing, we can state with complete confidence that Swift is the future of mobile development.
Developers are able to write safer, more reliable code, save time and cost to design amazing app experiences.
Despite the pessimism and conservatism of many companies based on a set of complaints that language is damp and there are problems with the Xcode - The App Solutions can declare - Swift passed the exam!