Introduction to the swift Programming Language

Source: Internet
Author: User

What is swift?

Swift is the programming language released by Apple in wwdc 2014. Here we reference the original words of the swift programming language:

Swift is a new programming language for ios and OS x apps that builds on the best of c and objective-c without the constraints of c compatibility.

Swift adopts safe programming patterns and adds modern features to make programming easier more flexible and more fun.

Swift's clean slate backed by the mature and much-loved cocoa and cocoa touch frameworks is an opportunity to imagine how software development works.

Swift is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.

To put it simply:

Swift is used to write ios and OS x programs. (It is estimated that it will not support other diaosi systems)

Swift draws on the advantages of c and objective-c and is more powerful and easy to use.

Swift can use the existing cocoa and cocoa touch frameworks.

Swift has both high performance and interactive scripting languages ).

Swift language Overview

Basic Concepts

Note: the code in this section is derived from a swift tour in the swift programming language.

Hello world

Similar to the scripting language, the following code is a complete swift program.

1

Println ("hello world ")

Variables and constants

Swift uses var to declare variables and let to declare constants.

1

2

3

Var myvariable = 42

Myvariable = 50

Let myconstant = 42

Type Derivation

Swift supports type inference, so the above Code does not need to specify the type. If you need to specify the type:

1

Let explicitdouble: double = 70

Swift does not support implicit type conversion (implicitly casting), so the following code needs explicit type conversion (explicitly casting ):

1

2

3

Let label = "the width is"

Let width = 94

Let width = label + string (width)

String formatting

Swift uses the \ (item) format for string formatting:

1

2

3

4

Let apples = 3

Let oranges = 5

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

Let applesummary = "I have \ (apples + oranges) pieces of fruit ."

Array and Dictionary

Swift uses the [] operator to declare arrays and dictionaries ):

1

2

3

4

5

6

7

8

Var shoppinglist = ["catfish" "water" "tulips" "blue paint"]

Shoppinglist [1] = "bottle of water"

Var occupations = [

"Malcolm": "captain"

"Kaylee": "mechanic"

]

Occupations ["jayne"] = "public relations"

Generally, the initializer syntax is used to create an empty array and an empty dictionary:

1

2

Let emptyarray = string [] ()

Let emptydictionary = dictionary <string float> ()

If the type information is known, you can use [] to declare an empty array and [:] to declare an empty dictionary.

Control Flow

Overview

Swift's condition statements include if and switch, and the loop statements include for-in, for, while, and do-while. The loop/judgment conditions do not need parentheses, but the loop/judgment body (body) required parentheses:

1

2

3

4

5

6

7

8

9

Let individualscores = [75, 43, 103, 87, 12]

Var teamscore = 0

For score in individualscores {

If score> 50 {

Teamscore + = 3

} Else {

Teamscore + = 1

}

}

Null type

Combined with if and let, you can easily process nullable variable ). For null values, you need to add? Explicitly indicate that this type can be empty.

1

2

3

4

5

6

7

8

Var optionalstring: string? = "Hello"

Optionalstring = nil

Var optionalname: string? = "John appleseed"

Var grespon= "hello! "

If let name = optionalname {

Grespon= "hello \ (name )"

}

Flexible switch

Switch in swift supports various comparison operations:

1

2

3

4

5

6

7

8

9

10

11

Let vegetable = "red pepper"

Switch vegetable {

Case "celery ":

Let vegetablecomment = "add some raisins and make ants on a log ."

Case "cucumber" "watercress ":

Let vegetablecomment = "that wocould make a good tea sandwich ."

Case let x where x. hassuffix ("pepper "):

Let vegetablecomment = "is it a spicy \ (x )? "

Default:

Let vegetablecomment = "everything tastes good in soup ."

}

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.