In 2014, Apple launched Swift, a new programming language for iOS mobile apps that’s given iOS developers an alternative to Objective-C, an object-oriented superset of the C programming language that’s been the core of iOS development thus far. Swift is designed to be compatible with all of the existing iOS development tools—xCode, Objective-C, and the Cocoa framework—but its ease of use and improved features mean it’s quickly starting to replace Objective-C. In this article, we’ll explore a few reasons why you should get on board with the Swift programming language for your next iOS app development project, if you haven’t already.
Swift is faster, easier, and lets developers be more productive.
Much of what the the OS X operating system is based on comes from inherited technology dating back to Apple’s beginnings, and OS X’s newer components have all been based on Objective-C—including mobile apps. Objective-C’s strengths lie in graphical user interfaces (GUIs) and its feature-rich frameworks, but compared with newer languages, it’s a clunky one to write.
As the hardware and software of the iOS operating system have evolved and improved with better memory and processors, Apple was able to develop a streamlined language that was better suited for fast-paced app development, and to also address some of Objective-C’s disadvantages. By shaking off some of the “baggage” that comes with a 30+-year-old language like C, Swift is designed to make iOS developers‘ lives easier and more productive.
Compatibility with the Cocoa Touch Framework & Objective-C language
Frameworks are libraries of code modules that make developing applications faster and more streamlined for developers. The frameworks associated with the Apple operating systems are Cocoa (for OS X) and Cocoa Touch (iOS). With modules written in the Objective-C language, Cocoa Touch is specifically geared toward touch-based devices like iPhones and iPads. So what does this mean for a new language like Swift?
Swift is completely compatible with Objective-C when developing apps in Cocoa Touch, the mobile framework for iOS. It works alongside Objective-C, so developers can interface between the two languages, create mixed-language apps, and take advantage of Cocoa Touch classes with Swift, and Swift classes with Objective-C. These interchangeable classes, design patterns, and APIs make it easy for a developer to pick and choose.Migrating code from Swift to Objective-C is easy, too. Developers can take advantage of Swift’s advanced features by replacing chunks of app code written in Objective-C with Swift.
Swift is designed to work with the Cocoa Touch framework; you’ll just need to set up a Swift development environment in Xcode. Then, import Cocoa frameworks, APIs, and Objective-C code modules to get started.
A few key advantages of Swift include:
- Swift runs faster—almost as fast as C++. And, with the newest versions of Xcode in 2015, it’s even faster.
- Unified files make code easier to maintain. Again, an old standard of the C language holds Objective-C back: a two-file requirement. This means that programmers have to update and maintain two separate files of code, whereas in Swift, these become one. That means less work for programmers, but not at the cost of speed on the front end.
- Better compilers = a better coding experience for programmers. Swift is built with the Low Level Virtual Machine (LLVM), a compiler that’s used by languages like Scala, Ruby, Python, C# and Go. The LLVM is faster and smarter than previous C compilers, so more workload is transferred from the programmer to Xcode and the compiler.
- No pointers means Swift is ‘safer.’ Objective-C, like other C languages, uses pointers—a method for exposing values that gives programmers more direct access to data. The problem with pointers is they can cause vulnerabilities in security. They also create a barrier to finding and fixing bugs. With Swift, however, if your code’s pointer is missing a value (a nil value), rather than continuing to run the app, it causes the app to crash and allows you to locate and fix bugs on the spot. You’ll have cleaner code and spend less time looking for bugs down the road.
- Better memory management. “Memory leaks” can occur in object-oriented programming, and apps, and they decrease available memory for an app to run causing the application to fail. Typically, Cocoa Touch APIs support Automatic Reference Counting (ARC), a streamlined way to handle memory management. But in the context of the Core Graphics API, ARC isn’t available—it’s up to the developer. This is a common pitfall when an app is using big data buffers, video, or graphics. When too much memory is used during a memory leak, an app can get shut down by the operating system. To fix this, Swift supports ARC across all APIs, and this stability means less time programmers have to spend focusing on memory management.
What is the future of Objective-C?
While many existing apps written in Objective-C will have to be maintained (meaning, Objective-C won’t be obsolete anytime soon), Apple’s existing APIs require a bit of code adaptation to work with Swift. Xcode updates have caused it to change along with new releases, as well.