We engineers are a picky bunch. We spend countless hours coding apps and websites. So, when it comes tools and programming languages, we have lots of opinions. With the recent announcement of a new language for iOS, Swift is being viewed as a pleasant, but unplanned surprise by many in the tech community. While general users will see little to no change in their mobile experience, it marks an entirely new direction for companies that create software.
Along with my fellow developers, I’ve been studying the new language to see how it compares with its 25-year-old predecessor, Objective-C. Apple claims it has no plans to abandon the older language. However, one can clearly see it will just be a matter of time before Objective-C quietly fades away.
1. LESS TYPING
Objective-C is widely known as a challenging, syntax heavy, “verbose” language that lacks many conveniences found in modern languages. To contrast, Swift is precise and expressive. For starters, the language combines your class declaration and implementation in a single file. While property modifiers still exist, your plethora of choices have been reduced to three types – strong, weak and unknown.
When it comes to object creation, much of the boilerplate code needed for creating classes has been removed. This means no required calls to super, init, alloc or new. Convenience initializers have also been refactored so they act more like overloaded methods than independent functions.
2. NO POINTER SYNTAX
This alone is cause for celebration! While the concept of Automatic Reference Counting has been previously added to Objective-C, Swift brings app development into the 21st century by eliminating retain cycles and autorelease pools completely. In addition to making your code more readable, Swift lets developers finally get back to design instead of debugging zombies and null pointer exceptions.
Swift rewrites the rules of iOS development, providing a single “sentinel” (e.g. nil) that can be applied to both objects and primitives. Variables written in Swift are also designated as optional or non-optional. Option types are enforced at compile-time (versus run-time). This makes the Swift compiler your test buddy – scanning code for unplanned nil assignments and cast exceptions as you type. Gone is the need to understand the complexities of nil, NULL and [NSNull Null].
Like Java, Swift makes use of generics to type check objects like arrays and dictionaries at compile time. This also reduces typing, unplanned nil assignments and cast exceptions. Fantastic.
5. ONE UNIFIED LANGUAGE
A language four years in the making, Swift has been designed to address the pitfalls of modern code development. Even though Objective-C has brought the mobile world to unprecedented heights, its mixed interpretation of C / C++ has been a stumbling block for many web, Windows and Java engineers. Swift addresses these concerns head-on and comes up with some innovative ways to get work done.
Wayne Bishop is a software developer who explores digital trends through product development and software engineering. He can be reached at @waynewbishop. He also writes regularly on his site, where this post first appeared.