Apple's Swift has far-reaching effects on all platforms, not just iOS, OS X, watchOS and tvOS. Learn why Swift matters, how to use the programming language and how it differs from Objective-C.
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: Apple's Swift programming language: Cheat sheet (free PDF) (TechRepublic)
What is Swift?
Swift is a language that is easier to learn and easier to read, giving it a lower barrier of entry when compared with older languages like Objective-C. This is one reason it's becoming the singular programming language for Apple Platforms (iOS, iPadOS, macOS, tvOS, watchOS) and beyond as it's compatible with Linux, servers and more.
Swift has grown tremendously since its inception and introduction at WWDC 2014, and the developer community around this language continues to grow.
SEE: 31 Mac keyboard shortcuts business users need to know (free PDF) (TechRepublic)
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. Swift is a new programming language created by Apple initially to supplement Objective-C and modernize Apple's programming language offerings, but as of 2021, is ultimately looking like a replacement to Objective-C at least for front-end development by third-party app developers. Apple has not at this point announced that Objective-C will be phased out; however, developers are choosing Swift over Objective-C when starting new projects or modernizing existing ones.
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.
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.
SEE: How to create Apple apps and widgets: Go from novice to expert with this training (TechRepublic Academy)
Be sure to take advantage of Apple's great Swift resources, which include these books about Swift that are available in the Apple Books app:
- The Swift Programming Language (Swift 5.5 edition)
- Intro to App Development with Swift
- Develop in Swift: Fundamentals
- Develop in Swift: Explorations
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.
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.
- Raspberry Pi: After launching five devices in less than a year, here's what they're doing next (TechRepublic)
- As developers consider quitting, here comes the next big skills crisis (TechRepublic)
- The way we teach coding is all wrong. Here's how it needs to change (TechRepublic)
- 10 ways to prevent developer burnout (free PDF) (TechRepublic)
- Job description: iOS developer (TechRepublic Premium)
- Interview questions: iOS developer (TechRepublic Premium)
What's new 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 new 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 new 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.
What's new 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.
What's new in Swift 5.3?
Swift 5.3 was released with Xcode 12 on Sept. 16, 2020. This release of Swift primarily focused on refinements to the language itself and expanding the Swift ecosystem.
The Swift group at Apple took the opportunity to optimize runtime performance by reducing the codesize for Swift apps (especially for SwiftUI apps), improving the memory optimizations and reducing the heap memory utilization.
On the developer side of things, Apple focused on making errors more precise and more actionable with tips in the compiler suggestions, code completion was improved more than 5x, and there's now better runtime error messages, making it easier to figure out issues when you encounter them during debugging.
In addition, Apple took into account many Swift Evolution suggestions from the community.
What's new in Swift 5.4?
Swift 5.4 was released on April 26, 2021 to all users of Xcode 12.5. On the improvements list there were many niceties, including compiler improvements, improved code completion when typing expressions and huge improvements for incremental compiles.
On the coding side, there are many additions from the Swift Evolution community, including:
- Improved Implicit Member Syntax (SE-0287).
- Multiple variadic parameters in function calls (SE-0284).
- Result builders (SE-0289).
- Local functions support overloading (SE-10069).
In addition, Swift 5.4 introduces the ability to name a variable the same as a function, and property wrappers are now supported for local variables.
Swift Package Manager improvements can also be found in Swift 5.4 and Xcode 12.5, including the ability to declare packages executable targets (SE-0294).
What's new in Swift 5.5?
Swift 5.5 was released in beta form with Xcode 13 on June 7, 2021, at WWDC 2021. The main improvements with this release center around concurrency with the introduction of the async/await mechanism of performing tasks that must perform work and wait for something to complete before continuing. Learn more about concurrency in Swift 5.5 by watching the WWDC 2021 session entitled "Explore structured concurrency in Swift" on the Apple Developer website, or by reading the Swift Concurrency documentation.
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 3.0 was released in May 2019, and was a significant update to the Swift Playgrounds app that has received stability updates and improvements—version 3.3.1 is now available. Version 3.0 is significant because it added the ability to import your own Swift files into a playground, and share Swift files amongst your playgrounds. It also includes coding error suggestions, dark mode support, additional tools for educators to build out their own Swift playground books and Swift version updates so the latest Swift code can be built inside of the Swift Playgrounds app.
With the introduction of Swift Playgrounds 4 at WWDC '21, Apple added the ability to create iOS apps within the Swift Playgrounds application on iPadOS 15. Doing this requires apps to be built with SwiftUI for the user interfaces and Swift for the programming language (you cannot use Objective-C or Interface Builder files in the build process). Apps built with the Swift Playgrounds app can be tested right on the iPadOS device being built with, and can even be shipped to the App Store. This new version of Swift Playgrounds for iPadOS will be available to iPad owners beginning this fall in the App Store.
Swift Playgrounds is a free app that can be downloaded for iPad from the App Store.
- 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, 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 beginning with iOS 13 and newer, 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
At WWDC 2020, Apple unveiled a new version of SwiftUI that added even more features for building entire iOS, macOS, tvOS, and watchOS apps using SwiftUI. They also took a leap forward for SwiftUI, making it the default way to build widgets for iOS 14. New features like outlines, grids and toolbars make SwiftUI even more powerful and capable than before.
WWDC '21 brought about a few new SwiftUI changes, including expanding compatibility with macOS to make more Mac developers switch to the new UI layout system that SwiftUI brings about. Many of the changes rely on bringing more standard components from the Mac interface over to SwiftUI, including the multi-column view, and search views for macOS and iOS and a new low-level graphics layout view compatible with SwiftUI to replace the aging drawRect from Swift and Objective-C.
Swift 5.5 and SwiftUI bring about other niceties into the language. One such new feature is called AsyncImage, which is a new view for remotely loading images. Other niceties include the ability to add swipe actions for list rows, pull to refresh and tidying up the SwiftUI API for commonly used items.
Beyond the session videos above, which are still relevant, Apple also released new session videos at WWDC 2021 that explains all of the new SwiftUI features:
What is Swift Package Manager?
In recent years, the Swift Package Manager has picked up popularity amongst iOS and macOS developers as other open source package managers have waned in the community. The Swift Package Manager, often abbreviated SPM, adds the ability starting in Swift 3.0 and Xcode 8 to bundle together a dependent Swift project or source files into a module that can easily be added and versioned by Git, then downloaded, built and embedded into an iOS, macOS, watchOS or tvOS project by Xcode.
This package manager is built into Xcode and adds an easy way to separate out reusable code that can be shared between multiple projects or multiple owners—in the case of open source or cross-company sharing of projects.
Building a module with SPM is easy and can be done with a few lines of code and git repository. Learn about creating and using a Swift Package in Xcode here, or learn more about how the system works on the Swift.org blog.
What is 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.
- Sept. 15, 2014: Apple releases Swift 1.0 with the Gold Master of Xcode 6.
- Oct.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.
- Sept. 15, 2015: Apple releases Swift 2.0 with the Xcode 7 Gold Master build.
- Oct. 20, 2015: Apple releases Swift 2.1 with the release of Xcode 7.1.
- Dec. 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.
- Sept. 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.
- Sept. 17, 2018: Apple releases Swift 4.2.
- March 25, 2019: Apple releases Swift 5.0.
- Sept.r 20, 2019: Apple releases Swift 5.1 with Xcode 11.
- March 24, 2020: Apple releases Swift 5.2.
- June 2020: Apple announces Swift 5.3 at WWDC '20.
- Sept. 16, 2020: Apple releases Swift 5.3 with Xcode 12.
Nov. 13, 2020: Apple releases Swift 5.3.1 with Xcode 12.1.
Dec. 15, 2020: Apple releases Swift 5.3.2 with Xcode 12.2.
Jan. 25, 2021: Apple releases Swift 5.3.3 with Xcode 12.3.
April 26, 2021: Apple releases Swift 5.4 with Xcode 12.5.
- May 25, 2021: Swift 5.4.1 released.
- Nov. 13, 2020: Apple releases Swift 5.3.1 with Xcode 12.1.
- Dec. 15, 2020: Apple releases Swift 5.3.2 with Xcode 12.2.
- Jan. 25, 2021: Apple releases Swift 5.3.3 with Xcode 12.3.
- April 26, 2021: Apple releases Swift 5.4 with Xcode 12.5.
- May 25, 2021: Swift 5.4.1 released.
- June 7, 2021: Apple reveals Swift 5.5 and SwiftUI 3, released with Xcode 13 beta.
June 28, 2021: Apple releases Swift 5.4.2.
Sept. 9, 2021: Swift 5.4.3 is released.
Sept. 20, 2021: Apple releases Xcode 13 with the Swift 5.5 programming language, including the much awaited (no pun intended) async/await functionality, and new SwiftUI features announced at WWDC '21.
- How to use Apple's Swift REPL from the Terminal (TechRepublic)
- How to create and use arrays in Apple's Swift programming language (TechRepublic)
Editor's note: This article has been updated to reflect the latest release of Swift.