Apps Android / IPhone Why Swift instead of Objective-C?
Articol Swift poza 2 couverture

Apps Android / IPhone

Why Swift instead of Objective-C?

In the article herein I would like to present the advantages of using Swift programming language for the development of iOS mobile applications.

WHY SWIFT?

Launched by Apple in June 2014, Swift is a much faster and better programming language than its predecessor, Objective-C. Swift 2 followed and after that Swift 3 was published in June 2016. Therefore, it is time to give up on Objective-C; and this is valid also for the most faithful partisans of this state of the art programme.
Leave your comfort zone and adopt Swift now!

Here, at Beler Soft, I have adopted Swift since it was launched. I remember that it took me a while to get accustomed to it, being familiar with Objective-C, but the results started to show even from the first days.
The source code is much better structured and organized, functionalities can be added much easier and errors are better managed, the objective being that of solving them at compilation and not when running the programme.

Programmers often wonder about the following:
– Should I learn Swift?
– Is Swift stable?
– Is Swift faster than Objective-C?

To all these questions, the answer is YES!
You should learn Swift no matter if you are an experienced programmer or you just started developing iOS applications.

THE ADVANTAGES OF SWIFT

Why is Swift so strong and amazing?
– It is secure
– It is stable
– It is a multi-paradigm programming language
– It has a clean syntax
– It is fast

Swift is an extremely secure programming language, featuring many security checks at compilation. Its main objective is that of solving as many issues as possible during compilation and not when the application is running.
An important thing which should be mentioned is entering optional variables. With their help we acknowledge the absence of one value – nil. The variable can either have value or not, this being optional.
For example:

var variable: String? // does not have any value
variable = ”Hello” // the variable now has a value

Swift also has a strong and very easy to use error treatment mechanism. The syntax is similar to that in other languages, but it functions differently. However, it contains the keywords: throw, catch and try:

• An error type object must be conform with the ErrorType protocol
enum ServerError : ErrorType {
case BadRequest,
case GeneralError(code: Int)
}

• Swift enums are most useful in presenting an error type group of objects
• In order to get rid of an error we use throw, and in order to catch an error we use try.

Other advantages from the security point of view are the following:
– memory security ensures variables initialization before they are used
– a two-phase initialization process with security checks
– overwriting of mandatory methods
– and many others …

Swift is a multi-paradigm programming language. It offers support for various programming types, like object-oriented, protocol-oriented, functional, generic, block-structured, imperative and declarative programming.
The concept of protocol-oriented programming is not a new one, but Swift protocols have several special features. The general idea of protocol-oriented programming is to use protocols instead of types. Thus, a flexible system with easy to modify relations from their rigid types can be created.

Swift is designed for high performance and speed with the help of the following techniques:
– memory layout optimization
– creation of methods links during compilation
– optimization of programmes compilation

PERFORMANCE

We have two key characteristics of the source code:

– Code quality – it must be flexible, solid and based on a good architecture
– Code performance – the application must be fast

The most important task is to make the source code functional on a stable architecture. Obtaining high-performance may be complicated and dangerous. It is not advisable to optimise since the beginning, instead we must measure the performance of the source code. In this way we can optimize only the slow parts of the application.

High-performance is crucial due to the fact that it is directly related with the user’s experience. Users want instant results; they don’t want to wait for the page to load, to see a loading gif for a long time or a slow moving animation.

Obviously, year after year, mobile devices become more and more powerful, with a higher processing speed, larger memory, larger storage space and speed.
Even if performance problems seem irrelevant due to the hardware components, software complexity grows continuously. Much more complex data must be processed and stored. We must display much more complex animations and we must treat more and more operations.

In order to increase performance, a first method is to add more power to the devices. We can increase the number of servers in order to achieve better data management. But, we cannot ask customers to frequently update their mobile phones or to buy other new and more performant ones. The second method is to surgically treat the problem which causes the application performance to decrease. To do so, we must precisely identify its origin, for example a slow source code, and to improve or replace it.

WHICH ONE IS FASTER: OBJECTIVE-C, SWIFT OR C?

Many of us wonder how fast Swift is.
Let’s find out together!
We will run a first test.

We will sort an array with 100.000 aleatory generated numbers. We use the sort function from sdtlib library and measure time elapsed in each of the three programming languages.

en01language
According to Swift High Performance, Xcode 7.0, Swift 2

en02sort

We see that Swift is 14.5 times faster than Objective-C, and 2.3 times faster than C.
In other examples and experiments, C is usually faster than Swift, but Swift is much faster than Objective-C.
Below, we will compare the sort speed between Swift arrays and unsafe C arrays (UnsafeMutablePointer).

Unsafe C arrays
var cArray = UnsafeMutablePointer.alloc(count) // 30.000 integer numbers
Swift
var sArray:[Int] = []

And here are the results:

en03soermethod
According to Swift High Performance, Xcode 7.0, Swift 2

en04execution

We see that the speed is relatively similar in both cases. One disadvantage of using UnsafeMutablePointer is that help functions are not pre-set, we have to write them, while Swift already has sort or filter functions.
Another disadvantage is that memory must be manually assigned and freed, always facing the risk of accessing memory areas which do not belong to the programme.

Finally, we present the performance obtained with Swift by using different methods and collections types: sets, arrays or dictionaries.

en05entity
According to Swift High Performance, Xcode 7.0, Swift 2

The ideas to be remembered from this comparative analysis are the following:
– Inserting or eliminating objects from the arrays is done rapidly, but has a certain complexity O(n) which grows together with the number of elements from the array
– If we want to run a massive search of elements, we obtain better performance by using sets

THE COMPILATION PROCESS

The compilation process of the source code written in Swift is a very interesting one, as you will discover below:

en06compilation

First of all, the source code is transformed in AST (Abstract Syntax Tree). Then, it is transformed in SIL (Swift Intermediate Language), initially into a raw SIL language and then into a canonical state. Afterwards the source code is transformed in LLVM IR (Intermediate Representation). Finally, after optimizations, the source code is assembled and becomes executable for specific architectures.

CONCLUSIONS

We have seen together some performance tests, comparing Swift to other programming languages, and we acknowledged its celerity and advantages.

Remember my advice: it is important to optimize the source code only when it is necessary, and not since the beginning.

A solid architecture and a clean source code are the two characteristics essential for the development of good applications.

A performance optimization does not necessarily involve greater complexity of the source code. Choosing the appropriate data structures improves the performance of the application’s.

Swift is the programming language recommended by Apple for the development of native iOS applications, and it will gradually replace Objective-C.

The source code syntax is much more beautiful and easier to understand in Swift.

As Apple says, Swift is like Objective-C, without the C.
___________
References:
https://www.raywenderlich.com/123100/collection-data-structures-swift-2
https://yalantis.com/blog/is-swift-faster-than-objective-c/
Swift High Performance, Konstiantyn Koval, 2015, Packt Publishing

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Facebook
Google+
http://blog.beleringenierie.com/en/2016/07/30/pourquoi-swift-et-non-plus-objective-c/">
Twitter