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.
Objective-C has won the hearts of many. 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: 10 ways to prevent developer burnout (free PDF (TechRepublic)
- 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: More Apple-related news and tips (TechRepublic on Flipboard)
How does Swift differ 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.
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.
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.
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"
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.
- Meet Swift: Apple's next-gen programming language that may replace Objective-C (TechRepublic)
- Apple's Swift comes to Linux (ZDNet)
- Apple's Swift benchmark suite is now open source (ZDNet)
- Apple open sources Swift and makes a Linux port available (ZDNet)
How can I 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.
- iBook: The Swift Programming Language (Apple)
- iBook: Using Swift with Cocoa and Objective-C (Apple)
- Swift Developer Documentation (Apple)
- Download: The truth about MooCs and bootcamps—Their biggest benefit isn't creating more coders (TechRepublic)
What does Swift mean 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.
SEE: Free ebook: Executive's guide to Apple in the enterprise (TechRepublic)
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.
- Want a developer job? Time to learn Apple's Swift as demand skyrockets (ZDNet)
- IBM forges VMware pact, pushes more Swift developer tools (ZDNet)
- Vapor Swift Web Framework (GitHub)
- Perfect: Server Side Swift Framework (Perfect)
- The 10 hottest developer jobs of 2017 (TechRepublic)
- Job description: iOS developer (Tech Pro Research)
- Interview questions: iOS developer (Tech Pro Research)
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.
- Apple's Swift and Swift 2 explained: What it is and what it means (CNET)
- After Apple open sources it, IBM puts Swift programming in the cloud (ZDNet)
What's in Swift 5.0?
Swift 5.0 ushered in more improvements for the most popular language for iOS and Mac development by adding many standard library additions like performance improvements and more.
The biggest feature of Swift 5.0 was the declaration of ABI Stability and Binary Compatibility; both of these mean that the Swift standard libraries are incorporated in the releases of macOS, iOS, tvOS, and watchOS going forward. Those libraries no longer need to be included in each app's binary, and the app package can be much smaller.
For more information, read the the Swift group's blog post about Swift 5.0.
What is 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.
- Apple's Swift Playgrounds will make robots and drones dance (CNET)
- How to use the Swift Playgrounds iPad app to code on the go (TechRepublic)
- Apple's Everyone Can Code initiative brings Swift curriculum and iPads to schools (TechRepublic)
What is SwiftUI?
At WWDC in June 2019, Apple released SwiftUI, which is a new way to create UIs for Swift apps. This set of tools and APIs allow developers to write declarative Swift syntax to define and present a user interface, getting many new features that come with for free, including:
- Dynamic Type
- Dark Mode
- Localizations for common items
- iOS accessibility features
SwiftUI allows you to state what the interface should do, and iOS takes control in presenting that interface to your users. You can create a list of items, and then describe the alignment, font, and color for each item all in code without having to utilize Interface Builder.
Using the new Design Tools in Xcode 11, you can see what the code is presenting to the user. The live preview window will automatically sync with the code in the side-by-side editor to preview as you type.
SwiftUI is native on all Apple platforms running iOS 13, including iPhone, Mac, iPad, Apple Watch, and Apple TV. Best of all, a UI that you create for one device will automatically be translated (or can be translated with little work) to all platforms.
SwiftUI is also the way you can easily port your iOS apps to Mac by adding the Mac as a deployment option in your iOS project settings. This new framework will ultimately lead to mere accessible, more maintainable, more portable apps for Apple's platforms.
For more information on implementing SwiftUI in your applications, check out the following WWDC 2019 session videos:
- WWDC Session 224: Modernizing Your UI for iOS 13
- WWDC Session 237: Building Custom Views with Swift UI
- WWDC Session 240: SwiftUI On All Devices
- WWDC Session 219: SwiftUI on watchOS
- WWDC Session 238: Accessibility in SwiftUI
- Swift, Apple's language of choice, gets a major refresh (ZDNet)
- SwiftUI framework documentation (Apple)
- SwiftUI Tutorials (Apple)
Swift's 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.
- March 29, 2018: Apple releases Swift 4.
- September 17, 2018: Apple releases Swift 4.2.
March 25, 2019: Apple releases Swift 5.0.
- How to use Apple's Swift REPL from the Terminal (TechRepublic)
- How to create and use arrays in Apple's Swift programming language (TechRepublic)
- Apple's Growing Role in the Enterprise (Tech Pro Research)
Editor's note: This article was originally published in April 2016.