Why should we use Swift over Objective-C?
Swift vs Objective-C — which programming language should you choose for iOS app development? With the popularity of the iOS platform and the higher payment ability of Apple users, businesses focus their strategic efforts to deliver the highest quality products. The right tech stacks play an important role here. Therefore, with the introduction of Swift in 2014, lots of confusion appeared in the iOS development world.
On the one hand, Objective-C has proven to be stable and has been used for many years in development. Whereas the newest programming language, Swift, provides a lot of benefits and a more modern way of coding.
Wondering what the difference is between Swift and Objective-C? Which language better suits your project? Check out the detailed Swift vs Objective-C comparison further to make the best choice.
What are Objective-C and Swift Programming Languages in iOS?
Objective-C and Swift are programming languages that are used to code the Apple software products under macOS, iOS, tvOS, watchOS, CarPlay, and Linux (only for Swift) operating systems. These languages have the corresponding app programming interfaces (APIs) and frameworks (Cocoa, Cocoa Touch). Objective-C was created in the late 80s and impacted Smalltalk (for object-oriented syntax) and C (for non-object) programming languages. The newer language, Swift, is not based on the C language. Instead, it no longer has any inheritance and is just compilable with C libraries and frameworks. Swift offers more powerful tools and modern coding solutions for simpler, but expressive coding, app safety, and performance. As well, the language is intuitive, and thus easier to learn for new programmers.
Another difference Swift vs Objective-C is in the way the code is presented. Swift’s code is unified and has simpler syntax, whereas, working with Objective-C, the code is divided into 2 code blocks:
- header (interface) files (.h)
- implementation files (.m)
Objective-C was originally created by the company Stepstone, by Brad Cox and Tom Love, in 1980s. Further, the Objective-C trademark extended to Apple Computers. Since then, Objective-C has been a standard programming language for macOS and, further, iOS and other Apple platforms. The main characteristic lies in its maturity, since it has been used and tested for many years for Apple’s software.
In contrast, Swift is not limited to Apple OS. In 2015 it became an open source and cross-platform programming language. This takes away the unsafe pointer management and at the same time provides interaction with long-standing Objective-C and C code bases. These days, Apple Inc. promotes the use of Swift and provides constant language version updates.
The following table gives the general characteristics for Swift vs Objective- C.
Currently, the development world is arguing about which language Swift or Objective-C is better for iOS development. Many projects, systems, and APIs have been built in Objective-C.
Thus, let’s discover the pros and cons of each, starting with Objective-C.
iOS Objective-C: Pros & Cons
Let’s now check out the advantages of Swift over the Objective-C programming language. Both Swift and Objective-C are among the top 20 most widely used programming languages worldwide. Among all developers, 8.1% are specialized in Swift, while 7% are experienced in Objective-C.
Here is the Stack Overflow chart:
Advantages of Objective-C over Swift in 2018
1- Objective-C is an approved, well-tested language
As mentioned, Objective-C is reliable and has been used and tested by many developers. There are tons of projects written in Objective-C and there are many great third-party frameworks.
2- Objective-C is more stable
As Swift is still evolving as a language, it has regular version updates and applications written in Swift need to be migrated to the newest version. Logically, this takes time and money. When an app is written in Objective-C, these operations are unnecessary.
3- Easier usage of private APIs
In instances where a specific custom solution is necessary for the product (e.g. specific color change or integration), Objective-C is easier to connect with private APIs. As Swift is constantly developing, it is unstable and this fact may result in some crashes from these sorts of connections.
4- Objective-C is easily compatible with C++
As it has routes from a C language, products integrating parts of C or C++ code operate smoothly. On the contrary, Swift is incompatible with the C++ language.
Downsides of Objective-C in 2018
1- Harder to learn
Objective-C is a distinct language which differs in syntax from other programming languages. Its memory management is more complex and has the legacy necessary to be understood by C and Smalltalk. In brief, Objective-C has a higher barrier for new developers because of complicated syntax and coding conditions. If you are deciding whether to learn Objective-C or Swift — choose Swift. It is highly recommended to learn Swift, as it is more logical, easier to read and understand, and tailored specifically for Apple’s hardware.
2- Fewer qualified Objective-C developers
It can be complicated to find a proper Objective-C developer. Their number is modestly decreasing as new developers are learning mostly Swift and others are switching from Objective-C to Swift. Any programming skills require regular practice, so with time, a decrease in the number of skilled Objective-C programmers is predicted.
Recommendation for When To Use Objective-C
There are two options where it could be still preferable to use Objective-C:
1- If an app has already been written in Objective-C and has a huge code base (100,000+ code lines)
The most logical recommendation for using Objective-С is if the project is highly dependent on this language or the application has already been written in Objective-C and is functioning ( example: Facebook ).
2- If you are developing a framework use Objective-C
So far, Swift is not ABI (Application Binary Interface) stable, which corresponds to the correct communication among all the elements within machinery code. ABI needs to be stable if a binary code has been compiled with different versions of the language. As an example, PSPDFKit is a framework that employs Objective-C and is used to handle PDF documents. Thus, if you plan to build an SDK, Objective-C is a better option.
3- If C or C++ third-party frameworks are used extensively
Usually, it is a rare situation, but if these frameworks are required, it is recommended that you use Objective- C, as it is a subset of C and therefore worth considering.
Swift — New iOS Programming Language
Let’s now check out the advantages of Swift over the Objective-C programming language.
Benefits of Using Swift
1- Swift is faster
For starters, let’s discover the difference: Swift vs Objective-C performance.
The performance of Swift is almost the same as that of C++, which is considered the fastest in algorithm calculation arithmetics. Apple had this idea in mind and worked to improve the speed of Swift. For example, Swift 2.0 has beaten C++ in several computation algorithms, such as Mandelbrot algorithm. Objective-C is slower because it contains C API legacy.
Swift is faster than Objective-C, because it removed the limitations of C language and has been improved with the help of advanced technologies that were unavailable when C was developed. As mentioned by Apple, Swift was originally designed to operate faster.
Despite the fact that languages are different, they both integrate, and work with Cocoa and Cocoa Touch APIs, for all Apple platforms. Therefore, a regular app-user would not recognize the difference in operating speed between Objective-C vs Swift. Speed also depends on a programmer’s level and capabilities, since a slow app can be written in Swift as well.
2- Swift is safer
Nowadays, an app’s data security is a substantial characteristic of a successful product. The construction of Swift has been designed to exclude and avoid mistakes with the help of its features — generics, optionals, and type interference to achieve app stability. Therefore, apps developed in Swift are less prone to bugs and crashes.
3- Swift is more readable
As well, Swift, opposed to Objective-C, got rid of some symbols. For example:
- ; symbol is optional in Swift to end lines and can be excluded
-  symbols represent method calls. In Objective-C, were placed inside each other like so:[], making it more tangled. Instead, in Swift, a standard comma is used to separate these actions inside the parentheses — (
4- Swift has less code
Swift is a more compact language for programming. However, this fact doesn’t imply code simplicity, of course. At times, it can be very difficult to write, but it brings more benefits and is highly reusable. The latter point cannot be applied to Objective-C.
Let’s take a real example: there is a famous app called Lyft that was rewritten, from scratch, in Swift. The idea was quite risky because the team started in the early stages of the new language and worked along with the improvements in Swift. What was the bottom line? The app went from 75,000 lines of code to 25,000. This stunning change did not influence the performance of Lyft, and the customers experienced no difference in its function.
5- Swift is less error-prone
Swift’s syntax and language constructions exclude the several types of mistakes potentially possible in Objective-C. This control means fewer crashes and cases of unexpected behavior. This, however, does not prevent bad code from being written, of course, but a developer is better protected from making unwanted mistakes. This can be called “ quality control,” in a way.
All of this provides reasons to consider Swift as a safe programming language.
6- Swift integrates with memory management
What about Objective-C vs Swift memory management? ARC (Automatic Reference Counting), the memory management functionality inside the Clang compiler, manages the memory of digital objects. This is easily handled with Swift working across procedural and object-oriented code using Cocoa API. With regard to Objective-C, procedural code works with Corel Graphic API and it is the developer’s responsibility to control it.
Hence, in Swift coding, unified memory management developers should not give attention to each digital object, but rather concentrate on the general logic of the application and its features.
7- Swift is an open-source language
Swift was originally created for Apple platforms (iOS, OS X, watchOS, tvOS) and was further expanded to Linux. Its creators understood the fact that to build a remarkable modern programing language, it should be available for everyone. Swift helped to develop many third-party frameworks & tools and has resulted in a growing community.
We can take our recently developed Swift framework for interacting with RESTful web services, “Moya,” as an example.
As well, Swift code is more portable to other platforms than Objective-C could ever be.
There is also an initiative to make Swift Android-compatible. The pull request has already been created and approved by the author of Swift, Chris Lattner. It is an interesting fact that, so far, no one actually uses Swift in Android programming.
8- Swift has interactive coding
Swift Playgrounds has introduced new opportunities to developers. This tool makes it possible to test code on the spot without compiling big pieces of it or creating the whole app. Playgrounds visualizes data and programmers can quickly check and correct everything along with further development. It is especially applicable to custom views and code experiments. The latest Xcode IDE gives developers a large space for experimentation in the form of a simple editor (incl. panel with lines, images, final view).
9- Swift is closer to other platforms
This point is very important, especially when speaking about the cooperation between programmers building the same app on different platforms. Apple’s modern programming language is easier to understand for non-iOS developers and minimizes the time taken for additional explanations and clarifications. This influences the productivity of work positively.
Moreover, Swift can be used as a script language. It is an interesting solution for the iOS community to unify the writing of build scripts. At the time being, iOS developers are split with regard to this activity. Some of them write build scripts in Bash, while others use Ruby, Python, etc. Swift iOS programming corresponds to all modern needs and offers amazing opportunities. Do you agree that it is easier to use one (especially your “native”) language, rather than work with two simultaneously?
10- Swift is Apple’s ongoing focus
Apple Inc. is concentrating on evolving Swift as its core programming language. Recently, the WWDC (Worldwide Developers Conference) gave a detailed presentation on Swift 4.2 where they presented a number of great features and updates.
Moreover, Swift is expected to complete an important milestone in 2019.
Downsides to Using Swift
1- Slower compilation speed
Compilation (transforming source code into binary/ machinery code) speed is much slower in Swift projects.
As Swift is a more complex language and more feature-rich, it is harder to ensure that everything is exact. For now, the Swift source code has been adjusted with each version update. Each time, this costs time and money. On the other hand, Apple has mentioned that they are currently working on this factor and expect to offer a solution in 2019.
2- No ABI stability
Another main issue with Swift lies in its ABI instability. Similar to API, which defines all communication among all elements in a source code, ABI defines the communication rules for a machine code. This mostly matters in larger projects, when compiling part of the application with a few Swift versions. Therefore, in order to provide stable communication in a binary code of different compiler versions, ABI should be stable and well-structured. This can result in some problems with Swift frameworks across Swift versions.
3- Absence of C++ import
If your project is highly dependant on C++, Swift is not an iOS language that should be chosen for this task.
Swift Use Cases
LinkedIn, a social network for professionals moved from Objective-C to Swift in their native app development. In 2016, there was a great speech presenting the reasoning behind this decision for the company. The bottom line was that Swift was the better choice for smaller and start-up projects, as was easier to work with. On the other hand, within 3 years, Swift became more mature and solve its issues in the early stages.
Another company, Lyft, an on-demand transportation company, uses Swift for its native iOS app development.
Other well-known apps like Yahoo Weather, Clear, Hipmunk, WordPress and Firefox iOS apps are also using Swift.
Objective-C or Swift?
These days, Objective-C has fewer updates, most of which are for compatibility with Swift (e.g. easy import to the Swift code). Swift was created as an interoperable language, so it is completely compatible with Objective-C. This means it is possible to combine these languages in a project, but there are certain obstacles:
You may ask: is Objective-C still relevant these days?
Programming in Objective-C will not become obsolete any time soon because, thanks to its 20 years of existence, it has a large code base, number of apps maintained, and third-party framework with Objective-C at its core. These solutions and libraries are unlikely to be rebuilt from scratch with a new language. This is the only reason why Objective-C is better than Swift to some extent. Thus, Swift is not going to replace Objective-C, especially at this stage.
To sum it up, Objective-C, when compared to Swift, is a more outdated language and lacks modern tools and properties. Swift has many updates and ongoing growth plans, and the language receives great support from Apple. It is an easy-to-learn, secure, modern, and interactive programming language. Swift has simplified coding patterns as well as coding interface vs Objective-C.
Swift to code, swift to execute!
If you have any questions or comments feel free to leave a text here or email me at firstname.lastname@example.org