Swift (programming language)

Source: Internet
Author: User

The following excerpt from Wikipedia is quite comprehensive.

Swift (programming language)

(redirected from Swift language )

Jump to: Navigation , Search

See "Swift" for entries similar to the "Swift (programming language)" name.


Programming Paradigm

Multi- paradigm ( object-oriented , functional , directive , block - structured )

Release Time



Chris Ratner with Apple

implemented by


Mode system

Static , strong type , type inference

Language of Inspiration

objective-c ,Rust,Haskell,Ruby,Python,C #,CLU [1] , and D language [2]

Operating system




Common file name extensions

. Swift


official website

Swift, a programming language that supports multiple programming paradigms , was launched by Apple in 2014 to compose OS X and IOS app [3]. In 2014, when design Swift was launched by AppleWWDC , Apple intended to allow Swift and objective-c to coexist in Apple's Operating system [3].

Catalog [ hide ]

    • 1 History
    • 2 Features
      • 2.1 Types and variables
      • 2.2 libraries, Runtime and deployment
      • 2.3 Memory Management
      • 2.4 Debugging and other elements
      • 2.5 places like Objective-c
      • 2.6 different from the place of Objective-c
    • 3 Discussions
    • 4 Sample Programs
    • 5 Related Items
    • 6 References

History [ edit ]

In July 2010, Apple Developer Tools Division Director Chris Ratner began working on the design of the Swift programming language, and after a year of completing the basic architecture, he led a design team to participate. Swift has been in development for about 4 years and was published in June 2014.

Apple claims Swift is characterized by fast, modern, secure, interactive, and significantly better than the Objective-c language. Swift is compiled with LLVM and can use the existing Cocoa and Cocoa Touch frameworks. The Xcode playgrounds feature is the biggest innovation that Swift brings to Apple's development tools, which provides a powerful interactive effect that allows swift source code to display its running results in real time during the writing process. Ratner himself stressed that playgrounds was largely inspired by the idea of Brettes Victor (Bret Victor). [4]

features [ edit ]

Swift cancels the use of Objective C pointers and other unsafe accesses, and discards objective C's early application of the Smalltalk syntax, which is fully changed to the period notation (dot-notation). Like many scripting languages , Swift can infer variable types (var, variant). At the same time, it provides Java-like namespaces (namespace), generics (generic), operand overloading (operator overloading). Swift is simply described as "objective-c without C" (Objective-c without the C). [5]

Types and Variables [ edit ]

in the context of cocoa and cocoa touch, many common classes are placed under the foundation Kit Library, which contains the NSString string library (using Unicode) and the collection class Nsarray and Nsdictionary. OBJECTIVE-C provides syntax sugars (syntactic sugar) to allow these objects to be integrated in the same language. For example, the notation for merging between NSString is as follows:

NSString *str = @ "Hello,"; str = [ str stringbyappendingstring:@ "World");

in Swift, the accumulation of strings can be used in addition ( + ) The operand is done directly as a first-level citizen (first-class citizen), the above example can be simplified to

var str = "Hello,"; str + = "World"


the architecture of the past cocoa (and Cocoa Touch) is always divided into two versions, one variable (mutable), which can be changed at runtime (runtime), the other immutable (immutable), and its initial value immutable. For example, Nsarray and Nsmutablearray are two versions of an array. This tradition continues in the swift language world, but it is much simpler to use the LET keyword to set constants (constant variable). For variable (Mutable) objects, the var keyword is used. The swift language uses Var, like C # or Javascript , to define variables, but the features are different, and Swift does not assign the initial value directly to the variable, i.e. the variable does not have a default value, so it must be initialized before using the var variable. Otherwise, compile-time errors will occur.

for ease of use, Swift also provides a definition of optional to declare nil-free references. Declare optional only need to add a question mark (?) after the type. Can. Once the Var variable is declared as optional, its initial value will be defaulted to nil. Optional is essentially an enum, there are two types of definitions None and some, and nil is optional.none.

Library, runtime, and deployment [ edit ]

Swift can use the same runtime (runtime) for Mac OS and IOS platforms as object-c. This means that the SWIFT program can run on platforms that currently exist, including IOS 6 and OS X 10.8 that can run Swift. [7] More importantly, Swift and OBJ-C code can coexist within a single program, as is the case with C and C + +.

In order to secure a large number of developers and reuse existing code, Xcode 6 allows the import of the Objective-c file in app target to be used by swift and exposed to the Objective-c Bridge header file (bridging header) Swift. When a developer adds a Swift file to an existing OBJECTIVE-C application, Xcode automatically creates the header files. For example, a Swift-known class "MyClass" can be used to obj-c the way #import "myclass-swift.h". [8]

Memory Management [ edit ]

Swift uses automatic reference counting (ARC) to manage storage instead of the garbage collection functionality of the past Objective C. In the past in Objective-c, strong references (strong reference) were retain in non-arc, whereas weak references (weak reference) were assign in non-arc. In order to solve the problem of circular reference, Swift provides unowned, cannot be set to optional types, cannot add question mark (?) or an exclamation point (!).

Debugging and other elements [ edit ]

Xcode's debugging mechanism provides the REPL (Read-eval-print Loop, a terminology derived from Emacs) environment for the Swift language, which can be used to evaluate or interact with other programs using SWIFT syntax, so that swift programming has a similar Python interacts with Ruby interface (Interactive Shell).

A place like Objective-c [ edit ]

    • Basic numeric types (numeric types) are approximately the same (for example , Int, UInt, Float, Double)
    • a large number of C operations objects were moved out of Swift, but some new operands were introduced.
    • curly braces are used for group statements.
    • The assigned value of the variable uses the equals sign, but the comparison uses the "two consecutive equals" (= =) operand. There is also a new operand, "three consecutive equals" (= = =) is used to determine whether the constant or variable is an instance of the same object (instance).
    • the brackets ([], Square brackets) are used for the representation of an array, and after declaring the array, you can assign an index value (index) to access the element.
    • Control Statement (statement), For , while, if, switch very similar to ojbective-c, but with extended functions like for in polling for the collection (collection), Switch You can also accept cases condition values that are not integers, and so on.

different from the objective-c of the place [ edit ]

  • The statement sentence (statement) does not need to use a semicolon (';') As an exit, but the semicolon can be separated within one line as more than two statements.
  • header files are no longer required.
  • annotation by */*/Can be a nested (nested) annotation, meaning that there can be annotations in the annotations, in the past some C or C + + compilers do not support nested annotations.
  • strongly typed (strong type)
  • type inference or implied type (type inference)
  • supports generic programming .
  • The function is a first-class type (first-class object), which means that the function can be used as a parameter to other functions and return values.
  • operands can be redefined within a class (operand overloading). A new operand can be generated.
  • Unicodeis supported in all aspects of the string. Some characters can even be the name of a language.
  • Many of the C-language families used to be notorious for their strange grammar (Error-prone behaviors) also changed:
      • The pointer no longer exists.
      • Assignment (Assignments) no longer callbacks value. The correct notation is if (i==0) , which is generally prone to mis-written if (i=0) causes compile-time errors (compile-time error).
      • in the Switch no need to use it within the block Break narrative sentence. In addition, you need to have executable code behind the case (C or C + + can use multiple case consecutively without additional code), or a compilation error will occur.
      • both the variable and the constant are initialized, and the bounds of the array are clearly defined.
      • overflow (overflows) problem. The C language does not enforce the bounds of the integer type (signed integers), which often occurs at run time. Swift can get the maximum or minimum value by using the Max or Min property of the integer type.

discussion [ edit ]

Javaeye's founder, Robbin, commented: "It's not enough for programmers to be familiar with swift syntax for a day." The key is to provide advanced data types, simplifying the Cocoa class library, otherwise without Swift is no different. "[9]

up to 600 pages of the Swift programming Language can be downloaded in iBooks for free. [Ten]

Sample Program [ edit ]

Swift does not need to introduce a header file or write in main (), nor does it need to include a semicolon at the end of each line narrative (statement). For example, a variable is declared as follows:

var implicitinteger = -

var implicitdouble = 70.0

var explicitdouble : Double = -

the way to declare constants is as follows:

Let numberofapples = 3

Let numberoforanges = 5

Let applesummary = "I have \ (numberofapples) apples."

Let fruitsummary = "I have \ (numberofapples + numberoforanges) pieces of fruit."

Swift does not support implicit type conversions (implicitly casting), so there is an explicit type conversion between numbers and strings (explicitly casting):

Let table = "The width of this table is"

Let width = -

Let tablewidth = Table + String (width)

The following line is the Hello World Program:

println ("Hello, World")

You can use Unicode characters

Let people = ["new": "Xin", "green": 8 , "Jade" : "small" :

for (first name, age) in person {

println ("\ ( name) is the age of ( )." ")


Related Items [ edit ]

    • objective-c
    • D Language

References [ edit ]

  1. ^ Lattner, Chris. Chris Lattner ' s homepage. Chris Lattner. June 3, [June 3, 2014]. I started work on the Swift programming Language in July of 2010. I implemented much of the basic language structure, with only a few people knowing of its existence. A few other (amazing) people started contributing in earnest late on, and it became a major focus for the Apple Devel Oper Tools Group in July [...] drawing ideas from Objective-c, Rust, Haskell, Ruby, Python, C #, CLU, and far too many Others to list. "
  2. ^ Building assert () in Swift, Part 2: __file__ and __line__-Swift Blog-. Apple Developer. [September 2014]. "Swift borrows a clever feature from the D-language: These identifiers expand to the location of the caller when Evaluated in a default argument list. "
  3. ^ 3.0 3.1 Apple announces Swift, a new programming language For IOS.
  4. ^ Apple's new programming language Swift takes only 4 years to complete development
  5. ^ Weber, Harrison. Apple announces ' Swift, ' a new programming language for OS X & IOS. VentureBeat. June 2, the.
  6. ^ "Strings and Characters", Apple Inc.
  7. ^ "Do swift-based apps work on OS X 10.9/ios 7 and lower?", StackOverflow
  8. ^ "Swift and objective-c in the same Project", Apple Inc.
  9. ^ Fanke robbin
  10. ^ the Swift programming Language.

Swift (programming language)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.