First take: Apple’s Swift speeds programming

 

Connecting state and local government leaders

In offering leaner coding features, including the use of modules and concise syntax, the Swift programming language is challenging conventional coding with a combination of speed and ease of use.

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.  

X
This website uses cookies to enhance user experience and to analyze performance and traffic on our website. We also share information about your use of our site with our social media, advertising and analytics partners. Learn More / Do Not Sell My Personal Information
Accept Cookies
X
Cookie Preferences Cookie List

Do Not Sell My Personal Information

When you visit our website, we store cookies on your browser to collect information. The information collected might relate to you, your preferences or your device, and is mostly used to make the site work as you expect it to and to provide a more personalized web experience. However, you can choose not to allow certain types of cookies, which may impact your experience of the site and the services we are able to offer. Click on the different category headings to find out more and change our default settings according to your preference. You cannot opt-out of our First Party Strictly Necessary Cookies as they are deployed in order to ensure the proper functioning of our website (such as prompting the cookie banner and remembering your settings, to log into your account, to redirect you when you log out, etc.). For more information about the First and Third Party Cookies used please follow this link.

Allow All Cookies

Manage Consent Preferences

Strictly Necessary Cookies - Always Active

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Sale of Personal Data, Targeting & Social Media Cookies

Under the California Consumer Privacy Act, you have the right to opt-out of the sale of your personal information to third parties. These cookies collect information for analytics and to personalize your experience with targeted ads. You may exercise your right to opt out of the sale of personal information by using this toggle switch. If you opt out we will not be able to offer you personalised ads and will not hand over your personal information to any third parties. Additionally, you may contact our legal department for further clarification about your rights as a California consumer by using this Exercise My Rights link

If you have enabled privacy controls on your browser (such as a plugin), we have to take that as a valid request to opt-out. Therefore we would not be able to track your activity through the web. This may affect our ability to personalize ads according to your preferences.

Targeting cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising.

Social media cookies are set by a range of social media services that we have added to the site to enable you to share our content with your friends and networks. They are capable of tracking your browser across other sites and building up a profile of your interests. This may impact the content and messages you see on other websites you visit. If you do not allow these cookies you may not be able to use or see these sharing tools.

If you want to opt out of all of our lead reports and lists, please submit a privacy request at our Do Not Sell page.

Save Settings
Cookie Preferences Cookie List

Cookie List

A cookie is a small piece of data (text file) that a website – when visited by a user – asks your browser to store on your device in order to remember information about you, such as your language preference or login information. Those cookies are set by us and called first-party cookies. We also use third-party cookies – which are cookies from a domain different than the domain of the website you are visiting – for our advertising and marketing efforts. More specifically, we use cookies and other tracking technologies for the following purposes:

Strictly Necessary Cookies

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Functional Cookies

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Performance Cookies

We do not allow you to opt-out of our certain cookies, as they are necessary to ensure the proper functioning of our website (such as prompting our cookie banner and remembering your privacy choices) and/or to monitor site performance. These cookies are not used in a way that constitutes a “sale” of your data under the CCPA. You can set your browser to block or alert you about these cookies, but some parts of the site will not work as intended if you do so. You can usually find these settings in the Options or Preferences menu of your browser. Visit www.allaboutcookies.org to learn more.

Sale of Personal Data

We also use cookies to personalize your experience on our websites, including by determining the most relevant content and advertisements to show you, and to monitor site traffic and performance, so that we may improve our websites and your experience. You may opt out of our use of such cookies (and the associated “sale” of your Personal Information) by using this toggle switch. You will still see some advertising, regardless of your selection. Because we do not track you across different devices, browsers and GEMG properties, your selection will take effect only on this browser, this device and this website.

Social Media Cookies

We also use cookies to personalize your experience on our websites, including by determining the most relevant content and advertisements to show you, and to monitor site traffic and performance, so that we may improve our websites and your experience. You may opt out of our use of such cookies (and the associated “sale” of your Personal Information) by using this toggle switch. You will still see some advertising, regardless of your selection. Because we do not track you across different devices, browsers and GEMG properties, your selection will take effect only on this browser, this device and this website.

Targeting Cookies

We also use cookies to personalize your experience on our websites, including by determining the most relevant content and advertisements to show you, and to monitor site traffic and performance, so that we may improve our websites and your experience. You may opt out of our use of such cookies (and the associated “sale” of your Personal Information) by using this toggle switch. You will still see some advertising, regardless of your selection. Because we do not track you across different devices, browsers and GEMG properties, your selection will take effect only on this browser, this device and this website.