Swift (programming language)

Swift is a high-level general-purpose, multi-paradigm, compiled programming language created by Chris Lattner in 2010 for Apple Inc. and maintained by the open-source community.

Apple intended Swift to support many core concepts associated with Objective-C, notably dynamic dispatch, widespread late binding, extensible programming, and similar features, but in a "safer" way, making it easier to catch software bugs; Swift has features addressing some common programming errors like null pointer dereferencing and provides syntactic sugar to help avoid the pyramid of doom.

It was initially a proprietary language, but version 2.2 was made open-source software under the Apache License 2.0 on December 3, 2015, for Apple's platforms and Linux.

The introduction of module stability makes it possible to create and share binary frameworks that will work with future releases of Swift.

[21] Swift 5.5, officially announced by Apple at the 2021 WWDC, significantly expands language support for concurrency and asynchronous code, notably introducing a unique version of the actor model.

[22] Swift 5.9, was released in September 2023 and includes a macro system, generic parameter packs, and ownership features like the new consume operator.

[7] On June 2, 2014, the Apple Worldwide Developers Conference (WWDC) application became the first publicly released app written with Swift.

[27] The Swift Programming Language, a free 500-page manual, was also released at WWDC, and is available on the Apple Books Store and the official website.

The app is presented in a 3D video game-like interface which provides feedback when lines of code are placed in a certain order and executed.

[43][44][45] In January 2017, Chris Lattner announced his departure from Apple for a new position with Tesla Motors, with the Swift project lead role going to team veteran Ted Kremenek.

[50][51] The platforms Swift supports are Apple's operating systems (Darwin, iOS, iPadOS, macOS, tvOS, watchOS), Linux, Windows, and Android.

[55] Swift is a general purpose programming language that employs modern programming-language theory concepts and strives to present a simple, yet powerful syntax.

Concurrent programs can be written using async/await syntax, and actors isolate shared mutable state in order to eliminate data races.

[74] Alternatively, the @main attribute can be applied to a structure, class, or enumeration declaration to indicate that it contains the program's entry point.

if-let syntax provides syntactic sugar for checking for the existence of an optional value and unwrapping it at the same time.Functions are defined with the func keyword.

Here is an example of an integer being matched against a number of potential ranges:for-in loops iterate over a sequence of values:while loops iterate as long as the given Boolean condition evaluates to true:Swift supports closures, which are self-contained blocks of functionality that can be passed around and used in code,[76] and can also be used as anonymous functions.

[80] Swift supports five access control levels for symbols: open, public, internal, fileprivate, and private.

As in C#,[82] Swift also includes syntactic sugar for this, allowing one to indicate a variable is optional by placing a question mark after the type name, var optionalInteger: Int?.

Swift offers built-in support for objects using either pass-by-reference or pass-by-value semantics, the former using the class declaration and the latter using struct.

Smaller structures, like a 2D point, can be defined as structs, which will be pass-by-value and allow direct access to their internal data with no indirection or reference counting.

[86] Extensions add new functionality to an existing type, without the need to subclass or even have access to the original source code.

[90] For instance, Swift uses extensions to add the Equatable protocol to many of their basic types, like Strings and Arrays, allowing them to be compared with the == operator.

This syntax allows programs to clearly call out potential suspension points and avoid a version of the Pyramid of Doom caused by the previously widespread use of closure callbacks.

await is again used to mark the point at which the program will suspend to wait for the completion of the async functions called earlier.Tasks and TaskGroups can be created explicitly to create a dynamic number of child tasks during runtime:Swift uses the Actor model to isolate mutable state, allowing different tasks to mutate shared state in a safe manner.

This takes the form of an additional header file that simply defines or imports all of the Objective-C symbols that are needed by the project's Swift code.

For instance, an Objective-C file in a mixed project called "MyApp" could access Swift classes or functions with the code #import "MyApp-Swift.h".

Not all symbols are available through this mechanism, however—use of Swift-specific features like generic types, non-object optional types, sophisticated enums, or even Unicode identifiers may render a symbol inaccessible from Objective-C.[98] Swift also has limited support for attributes, metadata that is read by the development environment, and is not necessarily part of the compiled code.

Self references to be treated as weak or unowned can be indicated using a capture list.A key element of the Swift system is its ability to be cleanly debugged and run within the development environment, using a read–eval–print loop (REPL), giving it interactive properties more in common with the scripting abilities of Python than traditional system programming languages.

An official "Server APIs" work group has also been started by Apple,[107] with members of the Swift developer community playing a central role.

[108] A second free implementation of Swift that targets Cocoa, Microsoft's Common Language Infrastructure (.NET Framework, now .NET), and the Java and Android platform exists as part of the Elements Compiler from RemObjects Software.