Swift (programming language)

term

programming

Kept in this browser · My desk

Apple's compiled language for its platforms, begun by Chris Lattner in July 2010 and announced on 2 June 2014 with the iOS 8 software development kit (SDK).

Lattner implemented much of the basic structure in secrecy before it became a major focus of Apple's Developer Tools group in July 2013; he cites ideas from Objective-C, Rust, Haskell, Ruby, Python, C# and CLU.

What it is. Swift is Apple's general-purpose compiled language for iOS, macOS and its other platforms, and since its open-source release also for Linux and, later, Windows. Apple presented it on 2 June 2014, inside the iOS 8 software development kit (SDK) with more than 4,000 new application programming interfaces, as combining "the performance and efficiency of compiled languages with the simplicity and interactivity of popular scripting languages", designed to eliminate entire categories of common programming errors and to coexist with Objective-C code, so that developers could add it to existing apps; Xcode Playgrounds displayed the output of Swift code instantly.

Origin. Chris Lattner, who had built the LLVM and Clang compiler infrastructure, started the language in July 2010 and implemented much of its basic structure in secret before it became a major focus of Apple's Developer Tools group in July 2013. His own page names the ideas he drew on: Objective-C, Rust, Haskell, Ruby, Python, C# and CLU. On 3 December 2015 Apple released Swift as open source under the Apache 2.0 licence with a runtime library exception: the compiler, debugger, standard library, Foundation libraries, package manager and REPL (read-eval-print loop), with support for Linux as well as iOS, OS X, watchOS and tvOS.

How it works: optionals. Optional types are part of the core. The Swift book says an optional represents two possibilities, either there is a value or there is none, and that nil in Swift is not a pointer, as it is in Objective-C, but the absence of a value of a certain type; a non-optional constant or variable cannot hold nil at all. Code must unwrap an optional before using it: with optional binding, with optional chaining through the ?. operator, with a fallback value given by ??, or with a forced unwrap, the ! operator, which stops the program if the value is missing. Rust's reference credits Swift for optional bindings.

How it works: memory. Swift manages class instances with Automatic Reference Counting (ARC), which frees an instance as soon as its last strong reference disappears. The book notes that reference counting applies only to instances of classes, because structures and enumerations are value types, copied rather than shared. ARC cannot reclaim a strong reference cycle, two instances that hold each other, so programmers break cycles by declaring one side weak or unowned; and because ARC frees memory at once rather than when a garbage collector runs, the book warns that weak references make poor caches. The Swift 6 announcement sums up the older guarantees: variables are initialized before use, memory is not accessed after it has been deallocated, and array indices are checked for out-of-bounds errors.

Standards and status. Swift's design changes go through the Swift Evolution process, in which a proposal is developed in a pitch thread on the forums and then put to an open review, while bug fixes and implementation follow ordinary code review. The Core Team sets the strategic direction and delegates evolution authority over areas to steering groups and workgroups, such as the Language Steering Group. Swift 6, generally available on 17 September 2024, added an opt-in language mode that extends the safety guarantees to data races in concurrent code, typed throws, signed and unsigned 128-bit integer types, a preview of Embedded Swift for microcontrollers with ARM and RISC-V bare-metal targets, and a single open-source Swift implementation of Foundation shared by Apple's platforms, Linux and Windows.

In practice. Swift builds apps for the platforms the 2015 release listed, iOS, OS X, watchOS and tvOS, and lives in the same projects as Objective-C code; the Swift package manager, part of the open-source release, builds libraries and tools; and Swift 6 expanded its support on Linux and Windows, with a new Swift Testing library across platforms.

What goes wrong. A forced unwrap of an empty optional is a crash at run time, which is safe for memory but not for availability. Strong reference cycles leak memory silently, since ARC never reclaims them. Before Swift 6 the compiler did not diagnose data races in concurrent code, and the Swift 6 mode that does so is opt-in, so code built in the older language modes keeps that exposure.

Neighbours. Objective-C, the older language of Apple's frameworks, with which Swift coexists; Kotlin, the language Google made first for Android, with nullability built into its types in the same way; Rust, which borrowed Swift's optional bindings; C and the ios platform. Not to be confused with the SWIFT interbank messaging network.

Also known as: swift, swiftlang

Sources

← Back to S Back to the glossary