Swift (programming language)

From Wikipedia for FEVERv2
Jump to navigation Jump to search

This article is about the Apple programming language. Swift (programming language)_sentence_0

For the scripting language, see Swift (parallel scripting language). Swift (programming language)_sentence_1

For the CPU core, see Apple A6. Swift (programming language)_sentence_2

Swift (programming language)_table_infobox_0

SwiftSwift (programming language)_table_caption_0
ParadigmSwift (programming language)_header_cell_0_0_0 Multi-paradigm: protocol-oriented, object-oriented, functional, imperative, block structured Declarative programmingSwift (programming language)_cell_0_0_1
Designed bySwift (programming language)_header_cell_0_1_0 Chris Lattner, Doug Gregor, John McCall, Ted Kremenek, Joe Groff, and Apple Inc.Swift (programming language)_cell_0_1_1
DeveloperSwift (programming language)_header_cell_0_2_0 Apple Inc. and open-source contributorsSwift (programming language)_cell_0_2_1
First appearedSwift (programming language)_header_cell_0_3_0 June 2, 2014; 6 years ago (2014-06-02)Swift (programming language)_cell_0_3_1
Stable releaseSwift (programming language)_header_cell_0_4_0 5.3.2
  / December 15, 2020; 3 days ago (2020-12-15)Swift (programming language)_cell_0_4_1
Preview releaseSwift (programming language)_header_cell_0_5_0 5.3 branchSwift (programming language)_cell_0_5_1
Typing disciplineSwift (programming language)_header_cell_0_6_0 Static, strong, inferredSwift (programming language)_cell_0_6_1
OSSwift (programming language)_header_cell_0_7_0 Apple's operating systems (Darwin, iOS, iPadOS, macOS, tvOS, watchOS), Linux, Windows, AndroidSwift (programming language)_cell_0_7_1
LicenseSwift (programming language)_header_cell_0_8_0 Apache License 2.0 (Swift 2.2 and later)

Proprietary (up to Swift 2.2)Swift (programming language)_cell_0_8_1

Swift (programming language)_header_cell_0_9_0 .swiftSwift (programming language)_cell_0_9_1
WebsiteSwift (programming language)_header_cell_0_10_0 Swift (programming language)_cell_0_10_1
Influenced bySwift (programming language)_header_cell_0_11_0
InfluencedSwift (programming language)_header_cell_0_12_0

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. and the open-source community, first released in 2014. Swift (programming language)_sentence_3

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. Swift (programming language)_sentence_4

Swift works with Apple's Cocoa and Cocoa Touch frameworks, and 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. Swift (programming language)_sentence_5

It is built with the open source LLVM compiler framework and has been included in Xcode since version 6, released in 2014. Swift (programming language)_sentence_6

On Apple platforms, it uses the Objective-C runtime library which allows C, Objective-C, C++ and Swift code to run within one program. Swift (programming language)_sentence_7

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. Swift (programming language)_sentence_8

Swift supports the concept of protocol extensibility, an extensibility system that can be applied to types, structs and classes, which Apple promotes as a real change in programming paradigms they term "protocol-oriented programming" (similar to traits). Swift (programming language)_sentence_9

Swift was introduced at Apple's 2014 Worldwide Developers Conference (WWDC). Swift (programming language)_sentence_10

It underwent an upgrade to version 1.2 during 2014 and a more major upgrade to Swift 2 at WWDC 2015. Swift (programming language)_sentence_11

Initially a proprietary language, version 2.2 was made open-source software under the Apache License 2.0 on December 3, 2015, for Apple's platforms and Linux. Swift (programming language)_sentence_12

Through version 3.0 the syntax of Swift went through significant evolution, with the core team making source stability a focus in later versions. Swift (programming language)_sentence_13

In the first quarter of 2018 Swift surpassed Objective-C in measured popularity. Swift (programming language)_sentence_14

Swift 4.0, released in 2017, introduced several changes to some built-in classes and structures. Swift (programming language)_sentence_15

Code written with previous versions of Swift can be updated using the migration functionality built into Xcode. Swift (programming language)_sentence_16

Swift 5, released in March 2019, introduced a stable binary interface on Apple platforms, allowing the Swift runtime to be incorporated into Apple operating systems. Swift (programming language)_sentence_17

It is source compatible with Swift 4. Swift (programming language)_sentence_18

Swift 5.1 was officially released in September 2019. Swift (programming language)_sentence_19

Swift 5.1 builds on the previous version of Swift 5 by extending the stable features of the language to compile-time with the introduction of module stability. Swift (programming language)_sentence_20

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

History Swift (programming language)_section_0

Development of Swift started in July 2010 by Chris Lattner, with the eventual collaboration of many other programmers at Apple. Swift (programming language)_sentence_22

Swift took language ideas "from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list". Swift (programming language)_sentence_23

On June 2, 2014, the Apple Worldwide Developers Conference (WWDC) application became the first publicly released app written with Swift. Swift (programming language)_sentence_24

A beta version of the programming language was released to registered Apple developers at the conference, but the company did not promise that the final version of Swift would be source code compatible with the test version. Swift (programming language)_sentence_25

Apple planned to make source code converters available if needed for the full release. Swift (programming language)_sentence_26

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. Swift (programming language)_sentence_27

Swift reached the 1.0 milestone on September 9, 2014, with the Gold Master of Xcode 6.0 for iOS. Swift (programming language)_sentence_28

Swift 1.1 was released on October 22, 2014, alongside the launch of Xcode 6.1. Swift (programming language)_sentence_29

Swift 1.2 was released on April 8, 2015, along with Xcode 6.3. Swift (programming language)_sentence_30

Swift 2.0 was announced at WWDC 2015, and was made available for publishing apps in the App Store in September 21, 2015. Swift (programming language)_sentence_31

Swift 3.0 was released on September 13, 2016. Swift (programming language)_sentence_32

Swift 4.0 was released on September 19, 2017. Swift (programming language)_sentence_33

Swift 4.1 was released on March 29, 2018. Swift (programming language)_sentence_34

Swift won first place for Most Loved Programming Language in the Stack Overflow Developer Survey 2015 and second place in 2016. Swift (programming language)_sentence_35

On December 3, 2015, the Swift language, supporting libraries, debugger, and package manager were open-sourced under the Apache 2.0 license with a Runtime Library Exception, and was created to host the project. Swift (programming language)_sentence_36

The source code is hosted on , where it is easy for anyone to get the code, build it themselves, and even create pull requests to contribute code back to the project. Swift (programming language)_sentence_37

In December 2015, IBM announced its Swift Sandbox website, which allows developers to write Swift code in one pane and display output in another. Swift (programming language)_sentence_38

The Swift Sandbox was deprecated in January 2018. Swift (programming language)_sentence_39

During the WWDC 2016, Apple announced an iPad exclusive app, named Swift Playgrounds, intended to teach people how to code in Swift. Swift (programming language)_sentence_40

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. Swift (programming language)_sentence_41

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. Swift (programming language)_sentence_42

During WWDC 2019, Apple announced SwiftUI, which provides a framework for declarative UI structure design across all Apple platforms. Swift (programming language)_sentence_43

Official downloads for the Ubuntu distribution of Linux have been available since Swift 2.2, with more distros added since Swift 5.2.4, CentOS and Amazon Linux. Swift (programming language)_sentence_44

There is an unofficial package for Android too. Swift (programming language)_sentence_45

Languages do not strictly support operating systems, the associated libraries (and compilers) do. Swift (programming language)_sentence_46

Swift is not officially supported by Android yet, but unofficial toolchains are available, such as the Swift Android Toolchain, keeping some compatibility with Apple's allowing cross-platform programs. Swift (programming language)_sentence_47

Platforms Swift (programming language)_section_1

The platforms Swift supports are Apple's operating systems (Darwin, iOS, iPadOS, macOS, tvOS, watchOS), Linux, Windows, and Android. Swift (programming language)_sentence_48

An for FreeBSD also exists. Swift (programming language)_sentence_49

Version history Swift (programming language)_section_2

Swift (programming language)_table_general_1

VersionSwift (programming language)_header_cell_1_0_0 Release DateSwift (programming language)_header_cell_1_0_1 macOSSwift (programming language)_header_cell_1_0_2 LinuxSwift (programming language)_header_cell_1_0_3 WindowsSwift (programming language)_header_cell_1_0_4
Swift 1.0Swift (programming language)_cell_1_1_0 September 9, 2014Swift (programming language)_cell_1_1_1 YesSwift (programming language)_cell_1_1_2 NoSwift (programming language)_cell_1_1_3 NoSwift (programming language)_cell_1_1_4
Swift 1.1Swift (programming language)_cell_1_2_0 October 22, 2014Swift (programming language)_cell_1_2_1 YesSwift (programming language)_cell_1_2_2 NoSwift (programming language)_cell_1_2_3 NoSwift (programming language)_cell_1_2_4
Swift 1.2Swift (programming language)_cell_1_3_0 April 8, 2015Swift (programming language)_cell_1_3_1 YesSwift (programming language)_cell_1_3_2 NoSwift (programming language)_cell_1_3_3 NoSwift (programming language)_cell_1_3_4
Swift 2.0Swift (programming language)_cell_1_4_0 September 21, 2015Swift (programming language)_cell_1_4_1 YesSwift (programming language)_cell_1_4_2 NoSwift (programming language)_cell_1_4_3 NoSwift (programming language)_cell_1_4_4
Swift 2.1Swift (programming language)_cell_1_5_0 October 20, 2015Swift (programming language)_cell_1_5_1 YesSwift (programming language)_cell_1_5_2 NoSwift (programming language)_cell_1_5_3 NoSwift (programming language)_cell_1_5_4
Swift 2.2Swift (programming language)_cell_1_6_0 March 21, 2016Swift (programming language)_cell_1_6_1 YesSwift (programming language)_cell_1_6_2 YesSwift (programming language)_cell_1_6_3 NoSwift (programming language)_cell_1_6_4
Swift 2.2.1Swift (programming language)_cell_1_7_0 May 3, 2016Swift (programming language)_cell_1_7_1 YesSwift (programming language)_cell_1_7_2 YesSwift (programming language)_cell_1_7_3 NoSwift (programming language)_cell_1_7_4
Swift 3.0Swift (programming language)_cell_1_8_0 September 13, 2016Swift (programming language)_cell_1_8_1 YesSwift (programming language)_cell_1_8_2 YesSwift (programming language)_cell_1_8_3 NoSwift (programming language)_cell_1_8_4
Swift 3.0.1Swift (programming language)_cell_1_9_0 October 28, 2016Swift (programming language)_cell_1_9_1 YesSwift (programming language)_cell_1_9_2 YesSwift (programming language)_cell_1_9_3 NoSwift (programming language)_cell_1_9_4
Swift 3.0.2Swift (programming language)_cell_1_10_0 December 13, 2016Swift (programming language)_cell_1_10_1 YesSwift (programming language)_cell_1_10_2 YesSwift (programming language)_cell_1_10_3 NoSwift (programming language)_cell_1_10_4
Swift 3.1Swift (programming language)_cell_1_11_0 March 27, 2017Swift (programming language)_cell_1_11_1 YesSwift (programming language)_cell_1_11_2 YesSwift (programming language)_cell_1_11_3 NoSwift (programming language)_cell_1_11_4
Swift 3.1.1Swift (programming language)_cell_1_12_0 April 21, 2017Swift (programming language)_cell_1_12_1 YesSwift (programming language)_cell_1_12_2 YesSwift (programming language)_cell_1_12_3 NoSwift (programming language)_cell_1_12_4
Swift 4.0Swift (programming language)_cell_1_13_0 September 19, 2017Swift (programming language)_cell_1_13_1 YesSwift (programming language)_cell_1_13_2 YesSwift (programming language)_cell_1_13_3 NoSwift (programming language)_cell_1_13_4
Swift 4.0.2Swift (programming language)_cell_1_14_0 November 1, 2017Swift (programming language)_cell_1_14_1 YesSwift (programming language)_cell_1_14_2 YesSwift (programming language)_cell_1_14_3 NoSwift (programming language)_cell_1_14_4
Swift 4.0.3Swift (programming language)_cell_1_15_0 December 5, 2017Swift (programming language)_cell_1_15_1 YesSwift (programming language)_cell_1_15_2 YesSwift (programming language)_cell_1_15_3 NoSwift (programming language)_cell_1_15_4
Swift 4.1Swift (programming language)_cell_1_16_0 March 29, 2018Swift (programming language)_cell_1_16_1 YesSwift (programming language)_cell_1_16_2 YesSwift (programming language)_cell_1_16_3 NoSwift (programming language)_cell_1_16_4
Swift 4.1.1Swift (programming language)_cell_1_17_0 May 4, 2018Swift (programming language)_cell_1_17_1 NoSwift (programming language)_cell_1_17_2 YesSwift (programming language)_cell_1_17_3 NoSwift (programming language)_cell_1_17_4
Swift 4.1.2Swift (programming language)_cell_1_18_0 May 31, 2018Swift (programming language)_cell_1_18_1 YesSwift (programming language)_cell_1_18_2 YesSwift (programming language)_cell_1_18_3 NoSwift (programming language)_cell_1_18_4
Swift 4.1.3Swift (programming language)_cell_1_19_0 July 27, 2018Swift (programming language)_cell_1_19_1 NoSwift (programming language)_cell_1_19_2 YesSwift (programming language)_cell_1_19_3 NoSwift (programming language)_cell_1_19_4
Swift 4.2Swift (programming language)_cell_1_20_0 September 17, 2018Swift (programming language)_cell_1_20_1 YesSwift (programming language)_cell_1_20_2 YesSwift (programming language)_cell_1_20_3 NoSwift (programming language)_cell_1_20_4
Swift 4.2.1Swift (programming language)_cell_1_21_0 October 30, 2018Swift (programming language)_cell_1_21_1 YesSwift (programming language)_cell_1_21_2 YesSwift (programming language)_cell_1_21_3 NoSwift (programming language)_cell_1_21_4
Swift 4.2.2Swift (programming language)_cell_1_22_0 February 4, 2019Swift (programming language)_cell_1_22_1 NoSwift (programming language)_cell_1_22_2 YesSwift (programming language)_cell_1_22_3 NoSwift (programming language)_cell_1_22_4
Swift 4.2.3Swift (programming language)_cell_1_23_0 February 28, 2019Swift (programming language)_cell_1_23_1 NoSwift (programming language)_cell_1_23_2 YesSwift (programming language)_cell_1_23_3 NoSwift (programming language)_cell_1_23_4
Swift 4.2.4Swift (programming language)_cell_1_24_0 March 29, 2019Swift (programming language)_cell_1_24_1 NoSwift (programming language)_cell_1_24_2 YesSwift (programming language)_cell_1_24_3 NoSwift (programming language)_cell_1_24_4
Swift 5.0Swift (programming language)_cell_1_25_0 March 25, 2019Swift (programming language)_cell_1_25_1 YesSwift (programming language)_cell_1_25_2 YesSwift (programming language)_cell_1_25_3 NoSwift (programming language)_cell_1_25_4
Swift 5.0.1Swift (programming language)_cell_1_26_0 April 18, 2019Swift (programming language)_cell_1_26_1 YesSwift (programming language)_cell_1_26_2 YesSwift (programming language)_cell_1_26_3 NoSwift (programming language)_cell_1_26_4
Swift 5.0.2Swift (programming language)_cell_1_27_0 July 15, 2019Swift (programming language)_cell_1_27_1 NoSwift (programming language)_cell_1_27_2 YesSwift (programming language)_cell_1_27_3 NoSwift (programming language)_cell_1_27_4
Swift 5.0.3Swift (programming language)_cell_1_28_0 August 30, 2019Swift (programming language)_cell_1_28_1 NoSwift (programming language)_cell_1_28_2 YesSwift (programming language)_cell_1_28_3 NoSwift (programming language)_cell_1_28_4
Swift 5.1Swift (programming language)_cell_1_29_0 September 10, 2019Swift (programming language)_cell_1_29_1 YesSwift (programming language)_cell_1_29_2 YesSwift (programming language)_cell_1_29_3 NoSwift (programming language)_cell_1_29_4
Swift 5.1.1Swift (programming language)_cell_1_30_0 October 11, 2019Swift (programming language)_cell_1_30_1 NoSwift (programming language)_cell_1_30_2 YesSwift (programming language)_cell_1_30_3 NoSwift (programming language)_cell_1_30_4
Swift 5.1.2Swift (programming language)_cell_1_31_0 November 7, 2019Swift (programming language)_cell_1_31_1 YesSwift (programming language)_cell_1_31_2 YesSwift (programming language)_cell_1_31_3 NoSwift (programming language)_cell_1_31_4
Swift 5.1.3Swift (programming language)_cell_1_32_0 December 13, 2019Swift (programming language)_cell_1_32_1 YesSwift (programming language)_cell_1_32_2 YesSwift (programming language)_cell_1_32_3 NoSwift (programming language)_cell_1_32_4
Swift 5.1.4Swift (programming language)_cell_1_33_0 January 31, 2020Swift (programming language)_cell_1_33_1 NoSwift (programming language)_cell_1_33_2 YesSwift (programming language)_cell_1_33_3 NoSwift (programming language)_cell_1_33_4
Swift 5.1.5Swift (programming language)_cell_1_34_0 March 9, 2020Swift (programming language)_cell_1_34_1 NoSwift (programming language)_cell_1_34_2 YesSwift (programming language)_cell_1_34_3 NoSwift (programming language)_cell_1_34_4
Swift 5.2Swift (programming language)_cell_1_35_0 March 24, 2020Swift (programming language)_cell_1_35_1 YesSwift (programming language)_cell_1_35_2 YesSwift (programming language)_cell_1_35_3 NoSwift (programming language)_cell_1_35_4
Swift 5.2.1Swift (programming language)_cell_1_36_0 March 30, 2020Swift (programming language)_cell_1_36_1 NoSwift (programming language)_cell_1_36_2 YesSwift (programming language)_cell_1_36_3 NoSwift (programming language)_cell_1_36_4
Swift 5.2.2Swift (programming language)_cell_1_37_0 April 15, 2020Swift (programming language)_cell_1_37_1 YesSwift (programming language)_cell_1_37_2 YesSwift (programming language)_cell_1_37_3 NoSwift (programming language)_cell_1_37_4
Swift 5.2.3Swift (programming language)_cell_1_38_0 April 29, 2020Swift (programming language)_cell_1_38_1 NoSwift (programming language)_cell_1_38_2 YesSwift (programming language)_cell_1_38_3 NoSwift (programming language)_cell_1_38_4
Swift 5.2.4Swift (programming language)_cell_1_39_0 May 20, 2020Swift (programming language)_cell_1_39_1 YesSwift (programming language)_cell_1_39_2 YesSwift (programming language)_cell_1_39_3 NoSwift (programming language)_cell_1_39_4
Swift 5.2.5Swift (programming language)_cell_1_40_0 August 5, 2020Swift (programming language)_cell_1_40_1 NoSwift (programming language)_cell_1_40_2 YesSwift (programming language)_cell_1_40_3 NoSwift (programming language)_cell_1_40_4
Swift 5.3Swift (programming language)_cell_1_41_0 September 16, 2020Swift (programming language)_cell_1_41_1 YesSwift (programming language)_cell_1_41_2 YesSwift (programming language)_cell_1_41_3 YesSwift (programming language)_cell_1_41_4
Swift 5.3.1Swift (programming language)_cell_1_42_0 November 13, 2020Swift (programming language)_cell_1_42_1 YesSwift (programming language)_cell_1_42_2 YesSwift (programming language)_cell_1_42_3 YesSwift (programming language)_cell_1_42_4
Swift 5.3.2Swift (programming language)_cell_1_43_0 December 15, 2020Swift (programming language)_cell_1_43_1 YesSwift (programming language)_cell_1_43_2 YesSwift (programming language)_cell_1_43_3 YesSwift (programming language)_cell_1_43_4

Features Swift (programming language)_section_3

Swift is an alternative to the Objective-C language that employs modern programming-language theory concepts and strives to present a simpler syntax. Swift (programming language)_sentence_50

During its introduction, it was described simply as "Objective-C without the baggage of C". Swift (programming language)_sentence_51

By default, Swift does not expose pointers and other unsafe accessors, in contrast to Objective-C, which uses pointers pervasively to refer to object instances. Swift (programming language)_sentence_52

Also, Objective-C's use of a Smalltalk-like syntax for making method calls has been replaced with a dot-notation style and namespace system more familiar to programmers from other common object-oriented (OO) languages like Java or C#. Swift (programming language)_sentence_53

Swift introduces true named parameters and retains key Objective-C concepts, including protocols, closures and categories, often replacing former syntax with cleaner versions and allowing these concepts to be applied to other language structures, like enumerated types (enums) Swift (programming language)_sentence_54

Closure support Swift (programming language)_section_4

Swift supports closures (known as lambdas in other languages). Swift (programming language)_sentence_55

Here is an example: Swift (programming language)_sentence_56

Swift has a trailing closure syntax like this: Swift (programming language)_sentence_57

Starting from version 5.3, Swift supports multiple trailing closures: Swift (programming language)_sentence_58

Here is the criteria for the trailing closure syntax: Swift (programming language)_sentence_59

Swift (programming language)_unordered_list_0

  • If the last arguments of a function are closures you can use the trailing closure syntax.Swift (programming language)_item_0_0
  • The parameter name of the first trailing closure must be omitted.Swift (programming language)_item_0_1
  • The parameter names of the remaining trailing closures must not be omitted.Swift (programming language)_item_0_2
  • If all the arguments given to a function are trailing closures, you may omit the parentheses after the function's name.Swift (programming language)_item_0_3
  • Calls to a function with trailing closures must be parenthesized if used in a guard statement.Swift (programming language)_item_0_4

String support Swift (programming language)_section_5

Under the Cocoa and Cocoa Touch environments, many common classes were part of the Foundation Kit library. Swift (programming language)_sentence_60

This included the NSString string library (using Unicode, UTF-8 in Swift 5, changed from UTF-16), the NSArray and NSDictionary collection classes, and others. Swift (programming language)_sentence_61

Objective-C provided various bits of syntactic sugar to allow some of these objects to be created on-the-fly within the language, but once created, the objects were manipulated with object calls. Swift (programming language)_sentence_62

For instance, in Objective-C concatenating two NSStrings required method calls similar to this: Swift (programming language)_sentence_63

In Swift, many of these basic types have been promoted to the language's core, and can be manipulated directly. Swift (programming language)_sentence_64

For instance, strings are invisibly bridged to NSString (when Foundation is imported) and can now be concatenated with the + operator, allowing greatly simplified syntax; the prior example becoming: Swift (programming language)_sentence_65

Access control Swift (programming language)_section_6

Swift supports five access control levels for symbols: open, public, internal, fileprivate, and private. Swift (programming language)_sentence_66

Unlike many object-oriented languages, these access controls ignore inheritance hierarchies: private indicates that a symbol is accessible only in the immediate scope, fileprivate indicates it is accessible only from within the file, internal indicates it is accessible within the containing module, public indicates it is accessible from any module, and open (only for classes and their methods) indicates that the class may be subclassed outside of the module. Swift (programming language)_sentence_67

Optionals and chaining Swift (programming language)_section_7

An important new feature in Swift is option types, which allow references or values to operate in a manner similar to the common pattern in C, where a pointer may refer to a value or may be null. Swift (programming language)_sentence_68

This implies that non-optional types cannot result in a null-pointer error; the compiler can ensure this is not possible. Swift (programming language)_sentence_69

Optional types are created with the Optional mechanism—to make an Integer that is nullable, one would use a declaration similar to var optionalInteger: Optional<Int>. Swift (programming language)_sentence_70

As in C#, 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 (programming language)_sentence_71

Variables or constants that are marked optional either have a value of the underlying type or are nil. Swift (programming language)_sentence_72

Optional types wrap the base type, resulting in a different instance. Swift (programming language)_sentence_73

String and String? Swift (programming language)_sentence_74

are fundamentally different types, the latter has more in common with Int? Swift (programming language)_sentence_75

than String. Swift (programming language)_sentence_76

To access the value inside, assuming it is not nil, it must be unwrapped to expose the instance inside. Swift (programming language)_sentence_77

This is performed with the ! Swift (programming language)_sentence_78

operator: Swift (programming language)_sentence_79

In this case, the ! Swift (programming language)_sentence_80

operator unwraps anOptionalInstance to expose the instance inside, allowing the method call to be made on it. Swift (programming language)_sentence_81

If anOptionalInstance is nil, a null-pointer error occurs. Swift (programming language)_sentence_82

This can be annoying in practice, so Swift also includes the concept of optional chaining to test whether the instance is nil and then unwrap it if it is non-null: Swift (programming language)_sentence_83

In this case the runtime only calls someMethod if anOptionalInstance is not nil, suppressing the error. Swift (programming language)_sentence_84

Normally this requires the programmer to test whether myValue is nil before proceeding. Swift (programming language)_sentence_85

The origin of the term chaining comes from the more common case where several method calls/getters are chained together. Swift (programming language)_sentence_86

For instance: Swift (programming language)_sentence_87

can be reduced to: Swift (programming language)_sentence_88

The ? Swift (programming language)_sentence_89

syntax circumvents the pyramid of doom. Swift (programming language)_sentence_90

Swift 2 introduced the new keyword guard for cases in which code should stop executing if some condition is unmet: Swift (programming language)_sentence_91

Using guard has three benefits. Swift (programming language)_sentence_92

While the syntax can act as an if statement, its primary benefit is inferring non-nullability. Swift (programming language)_sentence_93

Where an if statement requires a case, guard assumes the case based on the condition provided. Swift (programming language)_sentence_94

Also, since guard contains no scope, with exception of the else closure, leaseStart is presented as an unwrapped optional to the guard's super-scope. Swift (programming language)_sentence_95

Lastly, if the guard statement's test fails, Swift requires the else to exit the current method or loop, ensuring leaseStart never is accessed when nil. Swift (programming language)_sentence_96

This is performed with the keywords return, continue, break, or throw, or by calling a function returning a Never (e.g. fatalError()). Swift (programming language)_sentence_97

Objective-C was weakly typed and allowed any method to be called on any object at any time. Swift (programming language)_sentence_98

If the method call failed, there was a default handler in the runtime that returned nil. That meant that no unwrapping or testing was needed, the equivalent statement in Objective-C: Swift (programming language)_sentence_99

Would return nil, and this could be tested. Swift (programming language)_sentence_100

However, this also demanded that all method calls be dynamic, which introduces significant overhead. Swift (programming language)_sentence_101

Swift's use of optionals provides a similar mechanism for testing and dealing with nils, but does so in a way that allows the compiler to use static dispatch because the unwrapping action is called on a defined instance (the wrapper), versus occurring in the runtime dispatch system. Swift (programming language)_sentence_102

Value types Swift (programming language)_section_8

In many object-oriented languages, objects are represented internally in two parts. Swift (programming language)_sentence_103

The object is stored as a block of data placed on the heap, while the name (or "handle") to that object is represented by a pointer. Swift (programming language)_sentence_104

Objects are passed between methods by copying the value of the pointer, allowing the same underlying data on the heap to be accessed by anyone with a copy. Swift (programming language)_sentence_105

In contrast, basic types like integers and floating-point values are represented directly; the handle contains the data, not a pointer to it, and that data is passed directly to methods by copying. Swift (programming language)_sentence_106

These styles of access are termed pass-by-reference in the case of objects, and pass-by-value for basic types. Swift (programming language)_sentence_107

Both concepts have their advantages and disadvantages. Swift (programming language)_sentence_108

Objects are useful when the data is large, like the description of a window or the contents of a document. Swift (programming language)_sentence_109

In these cases, access to that data is provided by copying a 32- or 64-bit value, versus copying an entire data structure. Swift (programming language)_sentence_110

However, smaller values like integers are the same size as pointers (typically both are one word), so there is no advantage to passing a pointer, versus passing the value. Swift (programming language)_sentence_111

Also, pass-by-reference inherently requires a dereferencing operation, which can produce noticeable overhead in some operations, typically those used with these basic value types, like mathematics. Swift (programming language)_sentence_112

Similarly to C# and in contrast to most other OO languages, 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. Swift (programming language)_sentence_113

Structs in Swift have almost all the same features as classes: methods, implementing protocols and using the extension mechanisms. Swift (programming language)_sentence_114

For this reason, Apple terms all data generically as instances, versus objects or values. Swift (programming language)_sentence_115

Structs do not support inheritance, however. Swift (programming language)_sentence_116

The programmer is free to choose which semantics are more appropriate for each data structure in the application. Swift (programming language)_sentence_117

Larger structures like windows would be defined as classes, allowing them to be passed around as pointers. Swift (programming language)_sentence_118

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 dereference. Swift (programming language)_sentence_119

The performance improvement inherent to the pass-by-value concept is such that Swift uses these types for almost all common data types, including Int and Double, and types normally represented by objects, like String and Array. Swift (programming language)_sentence_120

Using value types can result in significant performance improvements in user applications as well. Swift (programming language)_sentence_121

To ensure that even the largest structs do not cause a performance penalty when they are handed off, Swift uses copy on write so that the objects are copied only if and when the program attempts to change a value in them. Swift (programming language)_sentence_122

This means that the various accessors have what is in effect a pointer to the same data storage. Swift (programming language)_sentence_123

So while the data is physically stored as one instance in memory, at the level of the application, these values are separate and physical separation is enforced by copy on write only if needed. Swift (programming language)_sentence_124

Protocol-oriented programming Swift (programming language)_section_9

A key feature of Objective-C is its support for categories, methods that can be added to extend classes at runtime. Swift (programming language)_sentence_125

Categories allow extending classes in-place to add new functions with no need to subclass or even have access to the original source code. Swift (programming language)_sentence_126

An example might be to add spell checker support to the base NSString class, which means all instances of NSString in the application gain spell checking. Swift (programming language)_sentence_127

The system is also widely used as an organizational technique, allowing related code to be gathered into library-like extensions. Swift (programming language)_sentence_128

Swift continues to support this concept, although they are now termed extensions, and declared with the keyword extension. Swift (programming language)_sentence_129

Unlike Objective-C, Swift can also add new properties accessors, types, and enums to extant instances. Swift (programming language)_sentence_130

Another key feature of Objective-C is its use of protocols, known in most modern languages as interfaces. Swift (programming language)_sentence_131

Protocols promise that a particular class implements a set of methods, meaning that other objects in the system can call those methods on any object supporting that protocol. Swift (programming language)_sentence_132

This is often used in modern OO languages as a substitute for multiple inheritance, although the feature sets are not entirely similar. Swift (programming language)_sentence_133

A common example of a protocol in Cocoa is the NSCopying protocol, which defines one method, copyWithZone, that implements deep copying on objects. Swift (programming language)_sentence_134

In Objective-C, and most other languages implementing the protocol concept, it is up to the programmer to ensure that the required methods are implemented in each class. Swift (programming language)_sentence_135

Swift adds the ability to add these methods using extensions, and to use generic programming (generics) to implement them. Swift (programming language)_sentence_136

Combined, these allow protocols to be written once and support a wide variety of instances. Swift (programming language)_sentence_137

Also, the extension mechanism can be used to add protocol conformance to an object that does not list that protocol in its definition. Swift (programming language)_sentence_138

For example, a protocol might be declared called StringConvertible, which ensures that instances that conform to the protocol implement a toString method that returns a String. Swift (programming language)_sentence_139

In Swift, this can be declared with code like this: Swift (programming language)_sentence_140

This protocol can now be added to String, with no access to the base class's source: Swift (programming language)_sentence_141

In Swift, like many modern languages supporting interfaces, protocols can be used as types, which means variables and methods can be defined by protocol instead of their specific type: Swift (programming language)_sentence_142

It does not matter what sort of instance someSortOfPrintableObject is, the compiler will ensure that it conforms to the protocol and thus this code is safe. Swift (programming language)_sentence_143

This syntax also means that collections can be based on protocols also, like let printableArray = [StringConvertible]. Swift (programming language)_sentence_144

As Swift treats structs and classes as similar concepts, both extensions and protocols are extensively used in Swift's runtime to provide a rich API based on structs. Swift (programming language)_sentence_145

For instance, Swift uses an extension to add the Equatable protocol to many of their basic types, like Strings and Arrays, allowing them to be compared with the == operator. Swift (programming language)_sentence_146

A concrete example of how all of these features interact can be seen in the concept of default protocol implementations: Swift (programming language)_sentence_147

This function defines a method that works on any instance conforming to Equatable, providing a not equals function. Swift (programming language)_sentence_148

Any instance, class or struct, automatically gains this implementation simply by conforming to Equatable. Swift (programming language)_sentence_149

As many instances gain Equatable through their base implementations or other generic extensions, most basic objects in the runtime gain equals and not equals with no code. Swift (programming language)_sentence_150

This combination of protocols, defaults, protocol inheritance, and extensions allows many of the functions normally associated with classes and inheritance to be implemented on value types. Swift (programming language)_sentence_151

Properly used, this can lead to dramatic performance improvements with no significant limits in API. Swift (programming language)_sentence_152

This concept is so widely used within Swift, that Apple has begun calling it a protocol-oriented programming language. Swift (programming language)_sentence_153

They suggest addressing many of the problem domains normally solved through classes and inheritance using protocols and structs instead. Swift (programming language)_sentence_154

Libraries, runtime and development Swift (programming language)_section_10

On Apple systems, Swift uses the same runtime as the extant Objective-C system, but requires iOS 7 or macOS 10.9 or higher. Swift (programming language)_sentence_155

It also depends on Grand Central Dispatch. Swift (programming language)_sentence_156

Swift and Objective-C code can be used in one program, and by extension, C and C++ also. Swift (programming language)_sentence_157

In contrast to C, C++ code cannot be used directly from Swift. Swift (programming language)_sentence_158

An Objective-C or C wrapper must be created between Swift and C++. Swift (programming language)_sentence_159

In the case of Objective-C, Swift has considerable access to the object model, and can be used to subclass, extend and use Objective-C code to provide protocol support. Swift (programming language)_sentence_160

The converse is not true: a Swift class cannot be subclassed in Objective-C. Swift (programming language)_sentence_161

To aid development of such programs, and the re-use of extant code, Xcode 6 and higher offers a semi-automated system that builds and maintains a bridging header to expose Objective-C code to Swift. Swift (programming language)_sentence_162

This takes the form of an additional that simply defines or imports all of the Objective-C symbols that are needed by the project's Swift code. Swift (programming language)_sentence_163

At that point, Swift can refer to the types, functions, and variables declared in those imports as though they were written in Swift. Swift (programming language)_sentence_164

Objective-C code can also use Swift code directly, by importing an automatically maintained header file with Objective-C declarations of the project's Swift symbols. Swift (programming language)_sentence_165

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". Swift (programming language)_sentence_166

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. Swift (programming language)_sentence_167

Swift also has limited support for attributes, metadata that is read by the development environment, and is not necessarily part of the compiled code. Swift (programming language)_sentence_168

Like Objective-C, attributes use the @ syntax, but the currently available set is small. Swift (programming language)_sentence_169

One example is the @IBOutlet attribute, which marks a given value in the code as an outlet, available for use within Interface Builder (IB). Swift (programming language)_sentence_170

An outlet is a device that binds the value of the on-screen display to an object in code. Swift (programming language)_sentence_171

On non-Apple systems, Swift does not depend on an Objective-C runtime or other Apple system libraries; a set of Swift "Corelib" implementations replace them. Swift (programming language)_sentence_172

These include a "swift-corelibs-foundation" to stand in for the Foundation Kit, a "swift-corelibs-libdispatch" to stand in for the Grand Central Dispatch, and an "swift-corelibs-xctest" to stand in for the XCTest APIs from XCode. Swift (programming language)_sentence_173

Memory management Swift (programming language)_section_11

Swift uses Automatic Reference Counting (ARC) to manage memory. Swift (programming language)_sentence_174

Apple used to require manual memory management in Objective-C, but introduced ARC in 2011 to allow for easier memory allocation and deallocation. Swift (programming language)_sentence_175

One problem with ARC is the possibility of creating a strong reference cycle, where objects reference each other in a way that you can reach the object you started from by following references (e.g. A references B, B references A). Swift (programming language)_sentence_176

This causes them to become leaked into memory as they are never released. Swift (programming language)_sentence_177

Swift provides the keywords weak and unowned to prevent strong reference cycles. Swift (programming language)_sentence_178

Typically a parent-child relationship would use a strong reference while a child-parent would use either weak reference, where parents and children can be unrelated, or unowned where a child always has a parent, but parent may not have a child. Swift (programming language)_sentence_179

Weak references must be optional variables, since they can change and become nil. Swift (programming language)_sentence_180

A closure within a class can also create a strong reference cycle by capturing self references. Swift (programming language)_sentence_181

Self references to be treated as weak or unowned can be indicated using a capture list. Swift (programming language)_sentence_182

Debugging and other elements Swift (programming language)_section_12

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. Swift (programming language)_sentence_183

The REPL is further enhanced with the new concept playgrounds. Swift (programming language)_sentence_184

These are interactive views running within the Xcode environment that respond to code or debugger changes on-the-fly. Swift (programming language)_sentence_185

Playgrounds allow programmers to add in Swift code along with markdown documentation. Swift (programming language)_sentence_186

If some code changes over time or with regard to some other ranged input value, the view can be used with the Timeline Assistant to demonstrate the output in an animated way. Swift (programming language)_sentence_187

In addition, Xcode has debugging features for Swift development including breakpoints, step through and step over statements, as well as UI element placement breakdowns for app developers. Swift (programming language)_sentence_188

Apple says that Swift is "an industrial-quality programming language that's as expressive and enjoyable as a scripting language". Swift (programming language)_sentence_189

Performance Swift (programming language)_section_13

Many of the features introduced with Swift have well-known performance and safety trade-offs. Swift (programming language)_sentence_190

Apple has implemented optimizations that reduce this overhead. Swift (programming language)_sentence_191

Comparisons to other languages Swift (programming language)_section_14

Swift is similar to C in various ways: Swift (programming language)_sentence_192

Swift (programming language)_unordered_list_1

  • Most C operators are used in Swift, but there are some new operators, for example to support integer operations with overflow (see under differences).Swift (programming language)_item_1_5
  • Curly braces are used to group statements.Swift (programming language)_item_1_6
  • Variables are assigned using an equals sign, but compared using two consecutive equals signs. A new identity operator, ===, is provided to check if two data elements refer to the same object.Swift (programming language)_item_1_7
  • Control statements while, if, and switch are similar, but have extended functions, e.g., a switch that takes non-integer cases, while and if supporting pattern matching and conditionally unwrapping optionals, for uses the for i in 1...10 syntax.Swift (programming language)_item_1_8
  • Square brackets are used with arrays, both to declare them and to get a value at a given index in one of them.Swift (programming language)_item_1_9

It also has similarities to Objective-C: Swift (programming language)_sentence_193

Swift (programming language)_unordered_list_2

  • Basic numeric types (Int, UInt, Float, Double)Swift (programming language)_item_2_10
  • Class methods are inherited, like instance methods; self in class methods is the class the method was called on.Swift (programming language)_item_2_11
  • Similar for...in enumeration syntax.Swift (programming language)_item_2_12

Differences from Objective-C include: Swift (programming language)_sentence_194

Swift (programming language)_unordered_list_3

  • Statements do not need to end with semicolons (;), though these must be used to allow more than one statement on a line.Swift (programming language)_item_3_13
  • No header files.Swift (programming language)_item_3_14
  • Uses type inference.Swift (programming language)_item_3_15
  • Generic programming.Swift (programming language)_item_3_16
  • Functions are first-class objects.Swift (programming language)_item_3_17
  • Enumeration cases can have associated data (algebraic data types).Swift (programming language)_item_3_18
  • Operators can be redefined for classes (operator overloading), and new operators can be defined.Swift (programming language)_item_3_19
  • Strings fully support Unicode. Most Unicode characters can be used in either identifiers or operators.Swift (programming language)_item_3_20
  • No exception handling. Swift 2 introduces a different and incompatible error-handling model.Swift (programming language)_item_3_21
  • Several features of earlier C-family languages that are easy to misuse have been removed:Swift (programming language)_item_3_22
    • Pointers are not exposed by default. There is no need for the programmer to keep track of and mark names for referencing or dereferencing.Swift (programming language)_item_3_23
    • Assignments return no value. This prevents the common error of writing i = 0 instead of i == 0 by throwing a compile-time error.Swift (programming language)_item_3_24
    • No need to use break statements in switch blocks. Individual cases do not fall through to the next case unless the fallthrough statement is used.Swift (programming language)_item_3_25
    • Variables and constants are always initialized and array bounds are always checked.Swift (programming language)_item_3_26
    • Integer overflows, which result in undefined behavior for signed integers in C, are trapped as a run-time error in Swift. Programmers can choose to allow overflows by using the special arithmetical operators &+, &-, &*, &/ and &%. The properties min and max are defined in Swift for all integer types and can be used to safely check for potential overflows, versus relying on constants defined for each type in external libraries.Swift (programming language)_item_3_27
    • The one-statement form of if and while, which allows for the omission of braces around the statement, is unsupported.Swift (programming language)_item_3_28
    • C-style enumeration for (int i = 0; i < c; i++), which is prone to off-by-one errors, is unsupported (from Swift 3 onward).Swift (programming language)_item_3_29
    • The pre- and post- increment and decrement operators (i++, --i ...) are unsupported (from Swift 3 onward), more so since C-style for statements are also unsupported from Swift 3 onward.Swift (programming language)_item_3_30

Development and other implementations Swift (programming language)_section_15

Since the language is open-source, there are prospects of it being ported to the web. Swift (programming language)_sentence_195

Some web frameworks have already been developed, such as IBM's Kitura, Perfect and Vapor. Swift (programming language)_sentence_196

An official "Server APIs" work group has also been started by Apple, with members of the Swift developer community playing a central role. Swift (programming language)_sentence_197

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

By combining toolchains from LLVM and Macintosh Programmer's Workshop, it is possible to run a very small subset of the language on Mac OS 9. Swift (programming language)_sentence_199

See also Swift (programming language)_section_16

Swift (programming language)_unordered_list_4


Credits to the contents of this page go to the authors of the corresponding Wikipedia page: en.wikipedia.org/wiki/Swift (programming language).