Skip to main content

Swift

Introduction

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community. Swift was developed as a replacement for Apple's earlier programming language Objective-C, as Objective-C had been largely unchanged since the early 1980s and lacked modern language features.

A key aspect of Swift's design was the ability to interoperate with the huge body of existing Objective-C code developed for Apple products over the previous decades. It is built with the open source LLVM compiler framework and has been included in Xcode since version 6, released in 2014. On Apple platforms, it uses the Objective-C runtime library, which allows C, Objective-C, C++ and Swift code to run within one program.

Although inspired by Objective-C and many other languages, Swift is not itself a C-derived language. As a complete and independent language, Swift packages core features like flow control, data structures, and functions, with high-level constructs like objects, protocols, closures, and generics. Swift embraces modules, eliminating the need for headers and the code duplication they entail.

Features

  • Closure support
  • String support
  • Access control
  • Optionals and chaining
  • Value types
  • Protocol-oriented programming
  • Libraries, runtime and development
  • Memory management
  • Debugging and other elements
  • Performance

Standard Library

Documentation for the standard library is presently hosted on the Apple Developer website. The Swift standard library defines a base layer of functionality for writing Swift programs, including:

  • Fundamental data types such as Int, Double, and String.
  • Common data structures such as Array, Dictionary, and Set.
  • Global functions such as print(:separator:terminator:) and `abs(:)`_.
  • Protocols, such as Collection and Equatable, that describe common abstractions.
  • Protocols, such as CustomDebugStringConvertible and CustomReflectable, that you use to customize operations that are available to all types.
  • Protocols, such as OptionSet, that you use to provide implementations that would otherwise require boilerplate code.

Swift Toolchains

Building

Swift toolchains are created using the script build-toolchain. This script is used by swift.org's CI to produce snapshots and can allow for one to locally reproduce such builds for development or distribution purposes. A typical invocation looks like the following:

  $ ./swift/utils/build-toolchain $BUNDLE_PREFIX

where $BUNDLE_PREFIX is a string that will be prepended to the build date to give the bundle identifier of the toolchain's Info.plist. For instance, if $BUNDLE_PREFIX was com.example, the toolchain produced will have the bundle identifier com.example.YYYYMMDD. It will be created in the directory you run the script with a filename of the form: swift-LOCAL-YYYY-MM-DD-a-osx.tar.gz.

Beyond building the toolchain, build-toolchain also supports the following (non-exhaustive) set of useful options::

  • --dry-run: Perform a dry run build. This is off by default.
  • --test: Test the toolchain after it has been compiled. This is off by default.
  • --distcc: Use distcc to speed up the build by distributing the c++ part of the swift build. This is off by default.
  • --sccache: Use sccache to speed up subsequent builds of the compiler by caching more c++ build artifacts. This is off by default.

More options may be added over time. Please pass --help to build-toolchain to see the full set of options.

Installing into Xcode

On macOS if one wants to install such a toolchain into Xcode:

  1. Untar and copy the toolchain to one of /Library/Developer/Toolchains/ or ~/Library/Developer/Toolchains/.

    Example :

  $ sudo tar -xzf swift-LOCAL-YYYY-MM-DD-a-osx.tar.gz -C /
$ tar -xzf swift-LOCAL-YYYY-MM-DD-a-osx.tar.gz -C ~/

The script also generates an archive containing debug symbols which can be installed over the main archive allowing symbolication of any compiler crashes.

  $ sudo tar -xzf swift-LOCAL-YYYY-MM-DD-a-osx-symbols.tar.gz -C /
$ tar -xzf swift-LOCAL-YYYY-MM-DD-a-osx-symbols.tar.gz -C ~/
  1. Specify the local toolchain for Xcode's use via Xcode->Toolchains.

Packages

There are a number of packages that are part of the core Swift project. One of them is:

Swift-DocC

DocC is a documentation compiler that makes it easy for you to produce documentation for your Swift frameworks and packages. The compiler builds your documentation by combining the comments you write in source with extension files, articles, and tutorials that live alongside your package’s source code. This documentation is for using the DocC tool to generate documentation for your project. DocC syntax — called documentation markup — is a custom variant of Markdown that adds functionality for developer-specific documentation features, like cross-symbol linking, term-definition lists, code listings, and asides. You add documentation markup to your source code, compile it with DocC, and produce reference documentation for your APIs. You can also use documentation markup, along with a set of directives that instruct how DocC generates your content, to offer step-by-step tutorials that teach developers to use your APIs through interactive coding exercises.

Tools

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution and use of “packages” of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies into target products.

The basic concepts that motivate the functionality of the Swift Package Manager:

  • Modules Swift organizes code into modules. Each module specifies a namespace and enforces access controls on which parts of that code can be used outside of the module A program may have all of its code in a single module, or it may import other modules as dependencies. Aside from the handful of system-provided modules, such as Darwin on macOS or Glibc on Linux, most dependencies require code to be downloaded and built in order to be used.
  • Packages A package consists of Swift source files and a manifest file. The manifest file, called Package.swift, defines the package’s name and its contents using the PackageDescription module A package has one or more targets. Each target specifies a product and may declare one or more dependencies.
  • Products A target may build either a library or an executable as its product. A library contains a module that can be imported by other Swift code. An executable is a program that can be run by the operating system.
  • Dependencies A target’s dependencies are modules that are required by code in the package. A dependency consists of a relative or absolute URL to the source of the package and a set of requirements for the version of the package that can be used The role of the package manager is to reduce coordination costs by automating the process of downloading and building all of the dependencies for a project.

Migration Guidelines

For users of Xcode, there is an included Swift migrator tool that helps you move your project to the latest version of Swift, or update it to work with the latest SDKs.

See Also :

  • For installation on Windows , Linux and Apple Platforms refer this link.
  • For downloading the latest version of Swift, click on this link.
  • For documentation , visit https://swift.org/documentation/.
  • For more information , visit the official website.