Apple

INDUSTRY INSIGHT

First take: Apple’s Swift speeds programming

Over the summer, Apple introduced a new programming language for creating applications for iOS and OS X devices.  As the first alternative to Objective-C, which has powered Apple application development since the mid-1990s, the debut of Swift is a noteworthy event. In a nutshell, Swift is designed to simplify development while enabling more responsive and robust applications.

For government – and Apple – the timing couldn’t be better, as federal agencies are shifting rapidly to a mobile-first mindset. This means that the mobile user experience is becoming the primary or default interface for all applications, government and commercial alike, creating new pressures to streamline and accelerate development. 

By simplifying development, it becomes easier to target niche requirements, allowing more users and missions to benefit from going mobile.

As a company long focused on iOS development, Agilex has worked with agencies as varied as Amtrak and Veterans Affairs to develop apps for both the iPhone and iPad.  Not surprisingly, we were interested to determine if Swift lived up to the hype. After using the language for over four months, it is clear that that Swift will have a significant impact on mobile application development.

Building responsive applications

It is difficult to program entire applications using a lower-level language such as C or C++.  However, these native languages are incredibly efficient in speed and memory usage, so they are great for algorithms.

Conversely, in a language like Java, it is easy to create front-facing user interface (UI) applications. But Java is slower and requires more resources than a lower-level language. This is because Java is not native code. It requires a Java Runtime Environment, which dynamically interprets bytecode – partially compiled Java code at runtime – and makes it slower.  

What makes Swift unique is that it is a compiled, native language, so it is as fast as other native languages like C or C++ and in many cases faster. In my testing, Swift is up to 35 times faster than the same algorithm in Objective-C. In real-world usage, Swift is about 7 times faster than Objective-C.

Swift is also very modern and makes it easy for developers to create user interfaces. In contrast, many applications for Android are written with Java and C++ (using the Android SDK and NDK), because neither language is particularly great for the entire application. Swift is challenging the paradigm of front-end and back-end languages thanks to its ease of use and speed.

Creating leaner code

Swift uses a clean and concise syntax along with modern programming concepts. At its core, Swift offers inferred types, which removes boilerplate that often pollutes code, allowing for extremely clean code. To continue with clean code, Swift has removed semi-colons that have become a standard across many programming languages.

Consider the same command written in Objective-C and Swift: 

Objective-C:

if (myDelegate != nil) {

    if([myDelegate respondsToSelector: @selector(scrollViewDidScroll:)]) {

        [myDelegate scrollViewDidScroll:myScrollView];

    }

}

Same thing in Swift:

myDelegate?.scrollViewDidScroll?(myScrollView)

Modules are also a new concept from Objective-C that remove the need for header files and import statements, while adding namespaces for greater name collision control. Swift also brings powerful use of closures (similar to lambdas in Java 8), tuples, multiple return values, generics and functional programming patterns, among others.

Eliminating bugs and errors

The most common reason applications crash is because of a “null” or “undefined” reference, caused by unsafe code. Swift was designed to eliminate these common mistakes with optionals.

Java developers are probably familiar with Google’s Guava library, which has its own optional framework. The Swift concept is similar, except that it is built into the language and is compiler-enforced so that these crashes do not happen. It is nearly impossible to write Swift code that can use an undefined reference.

Swift also makes strides with variables vs. constants that other languages lack. As with other languages, constants in Swift will enforce that the object will never change, but Swift takes it a step further when dealing with collections. Unlike Java, a constant collection will never have its contents changed. This means there is only one set of rules that are enforced at compile-time and they are consistent.

Rapid prototyping

One of the biggest promises made by Apple is the instantaneous nature of Swift through the use of Playgrounds. Previous “real-time” simulators, such as the Groovy Console (Groovy is a dynamic language built on top of Java) still require a compilation phase before executing the code.

Swift Playgrounds also offers the ability to compile on the fly while the programmer writes code to see real-time output. As the developer types code, the Playground will start executing and will output a line-by-line state as it executes. This allows developers to prototype a new algorithm and see the results in real-time, which will allow them to see programming issues much faster.

Adding Swift to development

So what are the downsides to adopting Swift? Swift cannot be used to create Android, Windows or web apps, and it will only run on devices running iOS 7 and newer or OS X Mavericks and newer.  Other than that, there is really no reason to continue writing new code in Objective-C.

What about existing projects that have lots of Objective-C code? Should developers run off and re-write them in Swift? No, as Swift is compatible with Objective-C within the same project. Swift and Objective-C code can live side-by-side in the same project. Swift can call Objective-C code and Objective-C can call Swift code, all through the use of Bridging Headers in the project.

That said, developers still need to know Objective-C as most open source libraries are written in the language. Likewise, a number of Cocoa APIs are written in Objective-C, so some interactions will not necessarily be “swift.”

But with the speed, safety and modern features offered by Swift there is little reason for developers to continue writing Objective-C code in new or existing projects.  

About the Author

Sean Kosanovich is a Software Engineer at Agilex supporting new product development.

inside gcn

  • Shutterstock ID: 415195669 By Flexey

    Early IPP test flights take off

Reader Comments

Fri, Nov 14, 2014

This company is attempting to make a Swift implementation for Android as well. I'm interested to see how it pans out. It would be nice to only need one language for smart phones/tablets, even if it needs to be tweaked between implementations. http://www.remobjects.com/elements/silver/

Please post your comments here. Comments are moderated, so they may not appear immediately after submitting. We will not post comments that we consider abusive or off-topic.

Please type the letters/numbers you see above

More from 1105 Public Sector Media Group