Swift Simple Introduction Tutorial

Source: Internet
Author: User

What is Swift?

Swift is the programming language that Apple announced in WWDC 2014. Here is the quote from the Swift programming language:

Swift is a new programming language for IOS and OS X apps This builds on the best of C and objective-c, without the Constr Aints 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, are an opportunity to imagine Ho W Software Development works.
Swift is the first industrial-quality systems programming language, is as expressive and enjoyable as a scripting Lang Uage.
To put it simply:

Swift is used to write iOS and OS X programs. (not expected to support other cock wire systems)
Swift draws on the strengths of C and Objective-c and is more powerful and easy to use.


Swift is able to use the existing cocoa and cocoa touch frameworks.


Swift combines the high performance (performance) and scripting language interactivity (Interactive) of the compiled language.
Swift Language Overview

Basic concepts

Note: The code in this section originates from the A swift tour in the swift programming language.

Hello, World

Similar to the scripting language. The following code is a complete Swift program.

println ("Hello, World") variables and constants
Swift declares a variable with Var and let declares a constant.

var myvariable = 42
myvariable = 50
Let myconstant = 42
Type deduction

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

Let explicitdouble:double = 70
Swift does not support implicit type conversions (implicitly casting), so the following code requires an explicit type conversion (explicitly casting):

Let label = "the width is"
Let width = 94
Let width = label + String (width)
String formatting

Swift uses the form of \ (item) to format the string:

Let apples = 3
Let oranges = 5
Let applesummary = "I has \ (apples) apples."
Let applesummary = "I had \ (apples + oranges) pieces of fruit."


Arrays and dictionaries

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

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:

Let Emptyarray = string[] ()
Let emptydictionary = dictionary<string, float> ()
Assume that the type information is known. You can use [] to declare an empty array. Use [:] to declare an empty dictionary.

Control flow

Overview

Swift's conditional statements include if and switch, loop statements include for-in, for, while, and do-while, loop/infer conditions do not require parentheses, but loop/infer body (body) required brackets:

Let individualscores = [75, 43, 103, 87, 12]
var Teamscore = 0
For score in Individualscores {
If score > 50 {
Teamscore + = 3
} else {
Teamscore + = 1
}
}
Nullable types

Combine if and let. can easily handle the nullable variable (nullable variable). For null values, do you need to add after the type declaration?

Explicitly indicates that the type is nullable.



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 wide variety of operations:

Let vegetable = "red pepper"
Switch Vegetable {
Case "Celery":
Let vegetablecomment = "Add some raisins and make ants on a log."
Case "cucumber", "watercress":
Let vegetablecomment = "so 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."
}
Other loops

For-in can also be used to traverse a dictionary in addition to traversing an array:

Let interestingnumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
For (kind, numbers) in Interestingnumbers {
For number in numbers {
If number > largest {
largest = number
}
}
}
Largest
While loops and Do-while loops:

var n = 2
While n < 100 {
n = n * 2
}
N
var m = 2
do {
m = m * 2
} While M < 100
M
Swift supports traditional for loops. Also can be combined by: (Generate an interval) and for-in implement the same logic.

var firstforloop = 0
For I in 0..3 {
Firstforloop + = i
}
Firstforloop
var secondforloop = 0
for var i = 0; I < 3; ++i {
Secondforloop + = 1
}
Note: Swift except for. And also... :.. Create a front-closing interval, while ... Generates a closed-back interval.

Swift Simple Introduction Tutorial

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.