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 Continue Reading
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.
This article is also available as a download, Cheat sheet: Apple’s Swift programming language (free PDF).
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: 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. 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.
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 these books about Swift that are available in the Apple Books app:
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.
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 “http://www.techrepublic.com/”http://www.techrepublic.com/” 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 September 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.
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:
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.
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.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.
- September 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.
- September 16, 2020: Apple releases Swift 5.3 with Xcode 12.
November 13, 2020: Apple releases Swift 5.3.1 with Xcode 12.1.
December 15, 2020: Apple releases Swift 5.3.2 with Xcode 12.2.
January 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.
- November 13, 2020: Apple releases Swift 5.3.1 with Xcode 12.1
- December 15, 2020: Apple releases Swift 5.3.2 with Xcode 12.2
- January 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
Editor’s note: This article has been updated to reflect the latest release of Swift.