Introduction to the Swift programming language

Source: Internet
Author: User
Tags arrays touch

What is Swift?

Swift is Apple's programming language in WWDC 2014, quoted by 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 Constra INTs 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-opportunity to imagine Software development works.

Swift is the industrial-quality Systems programming language This is as expressive and enjoyable as a scripting Lang Uage.

To put it simply:

Swift is used to write iOS and OS X programs. (It is not expected to support other cock-wire systems)

Swift absorbs the benefits of C and objective-c and is more powerful and easy to use.

Swift can use the existing cocoa and cocoa touch framework.

Swift has both the high performance (performance) and scripting language interactivity (interactive) of the compiled language.

Swift Language Overview

Basic concepts

Note: This section of code originates from 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 a variable, let declare a constant.

1

2

3

var myvariable = 42

myvariable = 50

Let myconstant = 42

Type inference

Swift supports type inference, so the above code does not need to specify a type, if you need to specify the type:

1

Let explicitdouble:double = 70

Swift does not support implicit type conversions (implicitly casting), so the following code requires an 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 form of (item) 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."

Arrays and dictionaries

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

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"

You typically use the initializer (initializer) syntax 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 declare an empty array using [], and declare an empty dictionary using [:].

Control flow

Overview

Swift's conditional statement contains if and switch, the loop statement contains For-in, for, while, and Do-while, and the loop/Judge condition does not require parentheses, but the circular/judgmental (body) required bracket:

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

}

}

Nullable types

Combined with if and let, the nullable variable can be easily handled (nullable variable). For null values, it needs to be added after the type declaration. explicitly indicate that the type is nullable.

1

2

3

4

5

6

7

8

var optionalstring:string? = "Hello"

Optionalstring = = Nil

var optionalname:string? = "John Appleseed"

var gretting = "Hello!"

If let name = optionalname {

gretting = "Hello (name)"

}

Flexible switch

The switch in Swift supports a variety of 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 = "This would 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."

}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.