When planning to make a new project for iOS or macOS, the developers often get a dilemma about which language to choose? Indeed, the creators of Swift worked on their brainchild extensively. They released an extremely light and thoroughly thought product, which is easy to master even for beginners. This is confirmed by the fact that only a year after its official release, Swift gradually began to catch up in popularity with the Objective-C.
However, not everything is this simple. Judging by the feedback of many experienced programmers, working with Swift imposes a number of limitations and inconveniences at the time of writing and compiling the code. In addition, its relative "novelty" and a number of fundamental innovations makes it rather problematic to perceive by developers that used Objective-C for many years. That's why some IT companies prefer the old fashioned Objective-C way.
To showcase the Swift vs Objective-C 2017 standoff, instead of studying the positive and negative experiences of other programmers, we are going to turn to the "dry" facts and analyze the total practices of individual IT companies. This article provides a detailed Swift vs Objective-C comparison, which shows the main advantages and disadvantages of a particular language.
Objective-C vs Swift differences
Below we would consider the main features of the difference between these two, essentially similarly predestined programming languages for startups.
Why use Objective-C?
- Large amount of existing libraries. Most of them are open-source and the diversity of designations allow to simplify the development by implementing the tried solutions to routine tasks.
- Simplification of the code through dynamic typing. Dynamic typing is inherent to Objective-C. How is this helpful in practice? Firstly, developers can easily use the familiar virtual functions. Secondly, the code lines written in this programming language do not require type casting to verify whether a particular object can perform the specified operation. This feature simplifies the interpretation of the developer's thinking through code. In addition, dynamic typing enhances flexibility in class usage. This feature facilitates the process of changing the structure and purpose of classes in the process of development.
- Habitual handling of exceptional situations. There are four directives in Objective-C - @try, @catch, @finally and @throw. They serve for the errors and exceptions handling. The most common example of using these directives is checking the correctness of the user data input. An application that does not provide exception handling, in such case, will shut down its work abnormally. In Swift, the enumeration is used for this. Using the enumeration the programmer foresees the possible outcomes: Success and Failure. Is it convenient? Perhaps. However, it is completely unusual for programmers who have a lot of experience with the older languages. In addition, designs with enum take up more space than exceptions, which are still used by Objective-C developers.
- Preprocessor usage. Preprocessor directives allow writing complex macros that facilitate the readability of code and allow beautiful expressions of complex constructs. The Swift compiler does not include preprocessors. This innovation, according to some developers, still has the right to life, though. In some cases, the absence of preprocessor directives reduces the likelihood of errors. Applications, written in Objective-C, often overflow with implicit type conversion errors. Whether this feature is an advantage, is for the developer to decide. However, as practice shows, many of them tend toward simple and more understandable code, and therefore, choose Objective-C.
- The high speed of static code analysis. The XCode framework working with Objective-C reacts incredibly quickly to the syntactic errors. In the case of Swift, this takes significantly longer. Experienced programmers often encounter the fact that they write the code much faster than its syntax is checked. Therefore, inadequate highlighting prompts sometimes irritate the developer, disrupting the workflow. It is possible that in future versions of compilers this defect will be eliminated. Nevertheless, currently, Objective-C wins in this account.
- Easy variables’ initialization. The creators of Swift took somewhat excessive care of the security of the developed code. That is why the initialization of variables is implemented in a quite complicated way. In particular, it has many rules and restrictions. For example, a programmer cannot access the properties’ values before they are initialized. From this point of view, the Objective-C is notably more understandable. In the absence of specific values for variables, the Objective-C developer can assign default or nil values for them, which is not available to the Swift developers.
- Support for older OS versions. Swift applications refuse to work in the old OS environments. Precisely, with the OS versions earlier than iOS 7.0 and macOS 10.9. Therefore, if aiming at building the universal projects that supports earlier versions of iOS and macOS, one would have to rely solely on the Objective-C.
- Adjustedness. Which language is the future - Swift or Objective-C? The answer to this question lies on the surface, seemingly. Swift was developed by Apple to simplify the code development and minimize the likelihood of errors. This is also demonstrated by practice: more than 30% of the applications created by means of Cocoa for the last three years, are written with Swift. Nevertheless, nothing ever is straightforward. Objective-C exists for more than 30 years, and it is logical that in the development environments that support this programming language, virtually all "pitfalls" are eliminated already. In its turn, Swift, which was released in 2014, is a fairly "raw" product.
Advantages of Swift over Objective-C
- Open-source. This means that there are no license fees for using Swift both in commercial and nonprofit projects.
- Syntax simplicity. The goal of Apple developers was to create an incredibly simple and understandable language. Indeed, Objective-C is scolded by many for the complexity of code structures. They can be quite difficult to interpret, not only by third-party and follow-up developers but also original authors. The Swift code in many cases looks much more beautiful and much less capacitive, which contributes to reduction of development timeframes. Another notable benefit is the ease of learning. An experienced developer can master this programming language from scratch in less than a month.
- Faster development. The Swift code is more compact, as mentioned. The amount of code for repeated declarations and strings is greatly reduced. In Objective-C, when working with text strings, you must be verbose. Swift contains all the features of the modern language, for example, adding 2 lines together with the operator "+", which is absent in Objective-C. The type system in Swift reduces the complexity in coding - since the compiler can figure out the types. Swift supports row interpolation, which eliminates the need to store tokens and allows programmers to insert variables, such as the name of a shortcut or a button, directly into the built-in user string. This reduces the number of errors that are common in Objective-C. In Objective-C, if you break the declaration order or use the wrong string token, your application will terminate. Swift again frees developers from the extra work: less code means fewer errors, thanks to the built-in support for text strings and data processing. As a result, Swift applications are developed faster.
- Default function call settings. Calling functions with a large number of parameters, among which there are preset ones, in Objective-C can be quite volumetric. In turn, a similar code written in Swift is more concise and easy for visual perception.
- Generics. Generics are the embodiment of universal functions that serve to process different data types. Objective-C uses other, more flexible mechanisms. However, this flexibility, due to the permissibility of using dictionaries and arrays with different types of objects, can result in the appearance of hard-to-find run-time errors. In Swift, only the exact matching types are allowed. Thus, the compiler performs automatic type checking, freeing the coder from unnecessary work.
- Namespace differentiation. The lack of namespace division support in Objective-C in many cases is fraught with a function, variable and file names’ collision glitches. Despite the introduction of two- and three-letter prefixes, which made it possible to accurately identify a particular module, they introduce some confusion still. In Swift, this problem is solved radically. Access to this or that file occurs by means of the namespace identifiers. Therefore, the risk that the open module would be connected to unrelated libraries and file systems is reduced to zero.
- No need for premature variable value designations. Optional types are used in Swift in cases where the value of a variable for some reason cannot be assigned at the moment of the coding. In such situations, the Objective-C implements a nil value. Practice shows that such techniques often entail emergency software shutdowns. In addition, for the programmer convenience, Swift developers provided some useful tools. These include conditional assignments and conditional calls. These language attributes allow a more concise and readable interpretation of the created algorithm into the code.
- Simplification of the code by higher-order functions. Using the higher-order functions is another way to reduce the code. Anonymous and nested functions, idioms and closures, as well as many other possibilities, in every possible way contribute to the transition from blocks with complex design peculiar to Objective-C, to single-line expressions.
- Maximization of application performance. The ARC, extremely detailed automatic reference counting function, minimizes the risk of memory leakages, which is a frequent companion of Objective-C applications. This applies to the procedural code. When working with the current version of Objective-C, the programmer is obliged to control the memory, allocated for the each digital object. Thus, at the time of the compilation, the additional resources are used. In the approach, characteristic to Swift, these are used more rationally - for example, in the case of the simultaneous multithreading.
- Optimization for some algorithm types. The Swift engine was designed to speed up the typical application tasks execution. In particular, as the special GeekBench test demonstrates, the productivity of the Mandelbrot algorithm implemented with the help of Swift, rivaled the performance of the С++ analog (which is generally higher than that of Objective-C). And, judging by the statements of the Apple developers, this is not the end. The following versions promise to work even faster.
- Support for dynamic libraries. Many developers that chose to stay with the Objective-C ignored one of the main Swift advantages - dynamic libraries support. Dynamic libraries greatly facilitate the future application updates. Thanks to this feature, developers receive the opportunity to connect applications written in older versions of the language compilator with newer ones, simply by providing an electronic signature and an NSA certificate. Another advantage of dynamic libraries is the reduction in the application size. Dynamic libraries are automatically included in the AppStore market’s download package and can be open source. This means that the end user spends less time downloading the product to their iPhone or other Apple manufactured device.
- Potential for Android application development. In April 2016, Google made a terrific announcement. Soon, Swift would be available for Android applications development. What does this mean for the IT market and workers in this industry? Definitely, the business of many IT companies will go up the hill. The cost of specialists offering development on Swift will also increase. This fact is further conditioned by the fact that Android holds for many years the world leadership among other mobile operating systems, partly due to its attractive design, moderate product prices and adaptability.
Both Swift and Objective-C have a number of custom merits. Despite the apparent simplicity, Swift advantages and disadvantages are often apprehended against it. In fact, Objective-C, thanks to its 30-year-old existence, is more thought-out. Therefore, it would not be surprising if many developers will prefer it for some time yet. Let us note that their choice is additionally supported by a favorable fact. If necessary, any startup transferring their development to Swift can use the existing Objective-C runtimes without problems. Thanks to this, it is possible to modify the already created applications and adapt them to the new arising environment.
Anyway, apparently Swift is the Apple’s chosen programming language. It is the future, it is being constantly developed and enhanced.The transition from Obj-C to Swift is already on the way. Even some large scale projects, like Lyft, are transferring their apps to Swift.