Sometimes, programming languages live longer than you would expect. However, developers cannot keep using outdated approaches. They need to stay up to date about all the latest developments and trends, which sometimes means making tough choices. When it comes to iOS development, one of the biggest questions is whether you should use Objective-C or Swift.
A Bit of History
Today, Objective-C is a language used to develop iPhone apps. However, this language was created back in the 1980s. Licensed by Steve Jobs’ NeXT Computer Inc., this language was used to develop NeXTStep frameworks. With time, it became the basis for many iconic products created by Apple. Objective-C is based on two languages: Smalltalk and C. It uses syntax from the C language for non-object-oriented operations and syntax from Smalltalk for object-oriented operations. One of the main advantages of Objective-C is that this language isn’t new and developers have tested it for many years.
Swift was released by Apple in 2014. According to Tim Cook, the new language was downloaded more than 11 million times within a month after its release. In 2015, Swift became the fastest growing language, according to the TIOBE Index. This language is free and available for everyone so there’s no surprise that it quickly became popular among iOS developers. Swift 5.0 was released in 2019. It has a stable binary interface that works well on different Apple platforms, including macOS, tvOS, and watchOS.
The Objective-C Foundation framework is the basis for many Swift features. For instance, Swift data is bridged to NSData. However, Swift also has a number of unique features that are absent in Objective-C.
After the release of Swift 5, its core libraries were integrated into iOS, macOS, watchOS, and tvOS releases. Therefore, apps created for these platforms can now be smaller due to included libraries. The stable application binary interface also allows Apple to provide support across platforms. Nevertheless, Apple continues to support Objective-C so many developers need to make a choice.
What language is more cost-effective for businesses and easier to work with? Here are some good reasons to choose Swift over Objective-C.
1. Swift is easy to read
Objective-C requires you to use keywords with the “@” symbol to make NSString literals. This way, the computer will be able to differentiate NSString objects from elements used in regular C. Given that Swift isn’t based on C, you don’t need to mark objects with anything so Swift can unify all the keywords. Here’s what code written in Objective-C and Swift looks like:
// Objective-C const int count = 10; double price = 23.55; NSString *firstMessage = @"Swift is awesome. "; NSString *secondMessage = @"What do you think?"; NSString *message = [NSString stringWithFormat: @"%@%@", firstMessage, secondMessage]; NSLog(message)
// Swift let count = 10 var price = 23.55 let firstMessage = "Swift is awesome. " let secondMessage = "What do you think?." let message = firstMessage + secondMessage print(message)
Swift also gets rid of legacy conventions. For instance, you don’t need to use parentheses for conditional expressions or semicolons to end lines. Swift uses a standard approach with lists and parameters within parentheses, separated by commas. As a result, Swift is a more expressive language. It is cleaner and has simplified grammar and syntax.
2. Swift is interactive
Thanks to Swift Playgrounds, developers can quickly test their code with no need to compile big pieces or to create a complete app. Playgrounds present data visually so that programmers can check and change their code on the spot. Thanks to the latest Xcode IDE, developers can experiment using a simple editor that includes a panel with images, lines, and the final view.
3. Swift is safer
When using Objective-C, nothing happens if you call a method with an uninitialized pointer variable. In this case, the expression becomes a no-operation. Although it doesn’t crash, it has caused many bugs because no-ops lead to unpredictable behavior.
Swift has a static type system. It ensures predictable behavior by triggering a runtime crash when the programmer uses a nil optional value. Thanks to this approach, the bug-fixing process becomes much easier because swift forces developers to fix any issues right away. The runtime crash stops on the line of code that contains a nil optional value so that bugs can be fixed faster.
4. Swift is easier to maintain
Objective-C cannot evolve if C doesn’t evolve. Programmers have to maintain two code files to improve the efficiency of executable app development and build time. Swift doesn’t require you to have two files. The LLVM compiler and Xcode can perform incremental builds automatically, understanding dependencies. Therefore, you can forget about the repetitive task of separating the implementation file from the header file. Swift replaces the implementation file
.m and header
.h with a single code file —
.swift. However, it will still generate an interface file that you can see in Xcode.
5. Unification with memory management
Swift supports Automatic Reference Counting (ARC) across object-oriented and procedural code paths. Although Objective-C supports ARC within the object-oriented code and Cocoa APIs, it’s still not available for other APIs, like Core Graphics, and procedural C code. Therefore, the programmer is responsible for handling memory management when working with low-level APIs.
Swift eliminates huge memory leaks that are common in Objective-C, enabling developers to focus on developing new features and core app logic. In Swift, ARC works across object-oriented and procedural code, even when dealing with lower-level APIs. Swift enabled Apple to solve the problem of high-performance and automatic memory management, increasing productivity. Besides, a Garbage Collector doesn’t clean up the unused memory, which is a very important factor in the context of user input and responsive graphics.
Although Apple still supports Objective-C for old-school developers, Swift offers many advantages that are impossible to ignore. It’s safer, it requires less code, and it’s simpler. Swift allows programmers to forget about many problems associated with outdated approaches used in Objective-C.
Developers who want to save time and money should choose Swift as a more efficient language.
Although programming languages die slowly, it makes sense to expect Swift to completely replace C for programming on Apple platforms. Swift not only inherited many useful components from Objective-C but also introduced a set of new features that allow developers to write more reliable code, enabling programmers to avoid lots of repetitive work and to focus on more global tasks.