Developer Open Source

Apple's Swift programming language: The smart person's guide

Apple's Swift has far-reaching effects on all platforms, not just iOS, OS X, watchOS, and tvOS. We dive into why Swift matters, how to use it, and how it differs from Objective-C.

Image: Apple

Objective-C has won the hearts of many developers. It started as a programming language that bundled object-oriented programming (OOP) with the likeness of the C programming language. Objective-C called NeXT and Apple home where it was the default programming language for NeXTSTEP, OS X, and iOS.

In 2010, Apple started developing Swift, a new programming language that would rival Objective-C in a few areas--specifically, type safety, security, and better on-hardware performance. Swift is more than 2.6x faster than Objective-C and more than 8.4x faster than Python. Swift 1.0 was released in September 2014.

SEE: Check out all of TechRepublic's smart person's guides

Executive summary

  • What is Apple's Swift? Swift is the newest programming language from Apple; it offers better type safety, security, and performance. Swift is available for developing iOS, OS X, watchOS, and tvOS apps.
  • Why does Apple's Swift matter? It's an easy programming language to learn, which is why many students, entry-level developers, and long-standing Mac and iOS developers are focusing their development skills on Swift. In addition, Apple open sourced Swift, making it available for developing on other platforms, not just those designed by Apple.
  • Who does Apple's Swift affect? Apps built with Swift can be run on iOS devices dating back to iOS 7 or later, and OS X devices dating back to OS X 10.9 or later.
  • When is Apple's Swift available? Swift is readily available in the most recent version of Xcode.
  • How can you get Apple's Swift? Get the latest version of Swift by downloading Xcode from the Mac App Store or the Apple Developer Center. Once Xcode is installed, Swift and the Objective-C compiler (LLVM) will be installed on your Mac.

SEE: Free ebook: Executive's guide to Apple in the enterprise (TechRepublic)

How Swift differs from Objective-C

While Apple hasn't announced plans to sunset the Objective-C language (in fact, Apple is still updating it), Swift is rapidly overtaking Objective-C to become more popular on Apple's platforms.

Objective-C likely still has a long life, as Apple has yet to update its own Frameworks to be written in Swift. Until Swift 3.0, Apple will not be including the Swift runtime on iOS or OS X, leaving Xcode to package together the runtime into the iOS or OS X app to ensure binary compatibility.

Swift does bring a lot of modern programming niceties to Apple's platforms. Here's a look at the main differences between Objective-C and Swift.

Header files

Objective-C was based on C, but added OOP paradigms to the language through object messaging. Because of that, Objective-C made use of header files that publicly declared its functions and definitions.

Swift gets rid of this practice, allowing developers to write a single .swift file that contains typical header information, properties, and all of the class defines into a single file. Say goodbye to header files, and hello to cleaner and leaner code.

REPL

Objective-C and Swift are compiled languages, despite Swift having a REPL (Read-Eval-Print Loop) for testing that typically only supports interpreted languages.

The REPL is available in the command line and Xcode. Inside of Xcode, it is known as Playgrounds, allowing developers to write Swift code, and have it evaluated immediately, printing out the results in the sidebar (Figure A). To create a new Swift Playground in Xcode, open Xcode and select File | New | Playground. A new window will appear, and you can enter Swift code to test things.

Figure A

Playgrounds are a place to evaluate and test code without creating an entire iOS or OS X project first. Image: Cory Bohon/TechRepublic

Type inference

With Objective-C and Swift, object types are strongly typed -- meaning, the compiler must know exactly what type of object (string, array, dictionary, custom object, etc.) needs to be stored in memory for a particular variable. With Swift, Apple decided to keep strong types, but allow the compiler to automatically infer the type based on the objects assigned to a variable.

This means you can write code like the following snippet, and Swift will see that myVariable is a String type, because it has a String assigned to it.

let myVariable = "some string here"

Developers can still perform strong typing for clarity by doing this, but it is unnecessary. However, it does add clarity, especially to developers that may be touching the codebase after you've worked on it.

let myVariable: String = "some string here"

Available platforms

When Apple announced Swift at WWDC 2014, the company reported that conference attendees were using one of the first publicly released Swift apps: the WWDC app. It was developed partially in Swift, and allowed attendees to view session schedules, maps, and more.

Officially, iOS 7 and higher support Swift; OS X 10.9 (Mavericks) and higher support it as well. All versions of watchOS and tvOS have support for running Swift-built apps.

Additional resources:

How to learn and use Swift

You'll need a Mac, and if you will be developing for iOS, watchOS, or tvOS, you'll need one of those devices as well.

After you download and install Xcode, the Objective-C and Swift compiler (LLVM) is installed on your Mac. At this point, opening Xcode will present options for beginning projects in either Swift or Objective-C.

The first place to start is with Playgrounds and the Swift REPL, where you can learn by playing with Swift's features in an environment that won't mess up any in-flight apps, and without the need to create any full testing iOS or OS X project.

Be sure to take advantage of Apple's great Swift resources, which include two books: one on getting started and one on integrating Swift with existing Objective-C apps.

Additional resources:

What Swift means for enterprises and developers

Swift is an open source language that is being adopted at a very rapid pace. It allows developers to prototype and write iOS, OS X, and other Apple platform apps faster and with fewer bugs and crashes than ever before.

This is significant for enterprises, especially businesses that have made investments in Apple's platforms, and investments in developing their own B2B/enterprise apps, because keeping those apps up to date has never been easier.

In addition, Swift being open sourced means that the language will be expanded to other platforms, beyond just what Apple has created. We are already seeing Swift being ported to Linux and other platforms.

One potential area where Swift will flourish is web apps. Swift being ported to Linux means that Swift apps can now run on low-cost, low-maintenance Linux servers that are already the cornerstone of existing web APIs and services. There are already frameworks for Swift that make it so Linux-based server apps can be built inside of Xcode, allowing businesses to utilize existing Swift developers to build the APIs and services that iOS and OS X apps often consume.

Additional resources:

What's in Swift 2.0

Apple released Swift 2.0 at WWDC '15, and it added many features and refinements. These are some of the new features in Swift 2.0.

  • Swift is open source with a new Linux port available.
  • A new error handling model using try, throw, and catch keywords.
  • It targets older versions of iOS and OS X. It's also safer, with the #available block that lets you wrap lines of code that will be executed on systems where the framework is available.
  • SDKs are now Swift-ier, thanks to Apple adding generics and nullability to existing Objective-C frameworks to make them interface better with Swift code.

What's in Swift 3.0

Apple released Swift 3.0 at WWDC ' 16; it was the first major release of Swift since Apple open sourced the language in December 2015. The new features in Swift 3.0 include:

  • Refinements to the core language and standard library to rid the language of NS prefixes and other Objective-C holdovers.
  • Major additions to the Linux port of Swift.
  • The addition of the Swift Package Manager to make it easier to manage dependencies.

What's in Swift 4.0

Like with other Swift releases, 4.0 was announced at WWDC '17 and released in beta form to developers alongside Xcode 9. This release of Swift added quite a few refinements and features to the programming language, including:

  • New Codeable protocol that allows for easy serialization of data wrapped in structs.
  • String literals can now break into multiple lines using the new """ declaration (three sets of quote marks) to open and close the multiline string.
  • Strings received a major overhaul, making them collections of characters.

Additional resources:

Swift Playgrounds for iPad

At WWDC '16, Apple announced the addition of Swift Playgrounds, an app that lets developers and beginning coders program using the Swift language on the iPad in a Playground environment that was pioneered on the Mac with Xcode 6.

At WWDC '17, Apple announced two new versions of Swift Playgrounds. Swift Playgrounds 1.5 is available immediately and provides the ability to interface with Bluetooth-connected devices like drones, Sphero, and LEGO toys to bring real-world programming for students and developers.

Also announced at WWDC '17 was Swift Playgrounds 2.0. This new version was made available as a beta to developers at the conference and features integrated API documentation tools, Swift 4.0 and Swift 3.2 support, support for the iOS 11 SDK, and support for using Camera and Augmented Reality APIs.

Swift Playgrounds is a free app that can be downloaded for iPad from the App Store.

Additional resources:

Version history

  • Mid 2010: Development begins
  • July 17, 2010: First Swift commit to the Swift GitHub repository
  • June 2, 2014: Apple announces Swift at WWDC 2014, giving developers a pre-release version of Swift and Xcode 6.
  • September 15, 2014: Apple releases Swift 1.0 with the Gold Master of Xcode 6.
  • October 15, 2014: Swift 1.1 is released with Xcode 6.1.
  • April 8, 2015: Swift 1.2 is released with Xcode 6.3
  • June 8, 2015: Apple announces Swift 2.0 at WWDC 2015, giving developers a pre-release version of Swift 2 and Xcode 7.
  • September 15, 2015: Apple releases Swift 2.0 with the Xcode 7 Gold Master build.
  • October 20, 2015: Apple releases Swift 2.1 with the release of Xcode 7.1.
  • December 3, 2015: Apple announces the Swift 3.0 roadmap on GitHub.
  • March 21, 2016: Apple releases Swift 2.2 with the release of Xcode 7.3.
  • September 13, 2016: Apple releases Swift 3.0 with the release of Xcode 8.
  • March 27, 2017: Apple releases Swift 3.1.
  • June 5, 2017: Apple announces Swift 4.0 at WWDC '17.

Additional resources:

Visit TechRepublic