Apple Swift programming language Introductory Tutorial "Chinese version"
Directory
1 Introduction
2 Getting Started with Swift
3 Simple values
4 Control Flow
5 Functions and closures
6 Objects and classes
7 Enumerations and structs
Next: Apple Swift programming language Introductory Tutorial "Chinese version", the last time to share the knowledge of the introduction of Swift, followed by the simple value of this piece of information.
3 Simple values
Use let to define constants, Var to define variables. The value of a constant does not need to be specified at compile time, but is assigned at least once. This means that you can use constants to name a value, and you find that you only need to be sure once, but in multiple places.
var myvariable = 42
myvariable = 50
Let myconstant = 42
Note
Gashero notes
The constant definition here is similar to a variable in a functional programming language and cannot be modified once it is assigned. A lot of use is good for health.
A constant or variable must have the same type as the assignment. So you don't have to strictly define the type. You can create a constant or variable by providing a value, and let the compiler infer its type. In the example above, compiling it would infer that myvariable is an integer type because its initialization value is an integer.
Note
Gashero notes
The type is bound to the variable name and belongs to the static type language. contributes to static optimization. It differs from Python, JavaScript, and so on.
If the initialization value does not provide enough information (or no initialization value), you can write the type after the variable name, separated by a colon.
Let Imlicitinteger = 70
Let imlicitdouble = 70.0
Let explicitdouble:double = 70
Note
Practice
Create a constant with a type of float and a value of 4.
Values are never implicitly converted to other types. If you need to convert a value to a different type, explicitly construct an instance of the desired type.
Let label = "the width is"
Let width = 94
Let Widthlabel = label + String (width)
Note
Practice
What error would you get if you tried to remove the string conversion from the last line?
There is also an easier way to include values in a string: Write the value in parentheses, and precede the parentheses with a backslash (""). For example:
Let apples = 3
Let oranges = 5//by Gashero
Let applesummary = "I has \ (apples) apples."
Let fruitsummary = "I had \ (apples + oranges) pieces of fruit."
Note
Practice
Use () to include a floating-point number to calculate to a string, and to include someone's name to greet.
Create an array and a dictionary using square brackets "[]", accessing its elements by means of an index or key in square brackets.
var shoppinglist = ["Catfish", "water", "tulips", "Blue paint"]
SHOPPINGLIST[1] = "Bottle of Water"
var occupations = ["Malcolm": "Captain", "Kaylee": "Mechanic",]
occupations["Jayne"] = "Public Relations"
To create an empty array or dictionary, use the initialization syntax:
Let Emptyarray = string[] ()
Let emptydictionary = dictionary<string, float> ()
If the type information cannot be inferred, you can write an empty array for "[]" and an empty Dictionary for "[:]", for example if you set a variable to know and pass in a parameter to the function:
Shoppinglist = []//GO shopping and buy something by Gashero
Apple Swift programming language Introductory Tutorial "Chinese version" 2