The following translation content is original, reproduced please specify:
From every day blog: http://www.cnblogs.com/tiantianbyconan/p/3768936.html
Break up and read ...
Swift is the new language that Apple has just released in WWDC, I have not learned, now look at the official Apple document translation while learning, plus the English level and the understanding of the programming is very limited, there is the wrong place please point out that translation for reference only, recommended to read Apple Swift official documents
Swift Tour
Traditionally, the first program to write when you start learning a new language should be to print "Hello, World" on the screen, which can be done in one line:
Print ("Hello, World")
If you have written a C or objective-c experience, you should feel familiar with this syntax-in swift, this line of code is a complete program. You don't need to import additional functional libraries like input and output streams or string processing. Code written at the global scope is the entry point of the program, so you don't need a main method. You also don't need to write a semicolon behind each statement.
This journey will give you enough information to use Swift to complete a variety of programming tasks. Don't worry if you know a lot about programming-this book is a detailed introduction to all aspects.
Basic data
Use let to represent a constant and use Var to represent a variable. The value of a constant does not need to be known at compile time, but you must and can assign only one value to it at a time. This means that once you assign a value to the constant, you can use it in many places.
var myvariable = 42;
myvariable = 50;
Let myconstant = 42;
The type of a constant or variable must be the same type as the value you assign to it. However, you generally do not need to specify the exact type. When you create a constant or variable and assign a value, the compiler automatically infers its type. In the example above, for example, the compiler infers that myvariable is an integer because its initial value is an integer.
If the initial value does not provide enough information (or it does not have an initial value at all), you can add a colon after the variable and write the specified type.
Let Implicitinteger = 70
Let implicitdouble = 70.0
Let explicitdouble:double = 70
Values are not implicitly converted to another type. If you need to convert to a different type of value, the display generates an instance of the desired type.
Let label = "the width is"
Let width = 94
Let Widthlabel = label + String (width)
There is also an easier way to include variable values in a string: Write a pair of parentheses and write a variable value, and then write a backslash in front of the parentheses, for example:
Let apples = 3
Let oranges = 5
Let applesummary = "I has \ (apples) apples."
Let fruitsummary = "I had \ (apples + oranges) pieces of fruit."
Use the brackets ([]) to create an array or dictionary, using Index (index) or key (key) to access one of their items.
var shoppinglist = ["Catfish", "water", "tulips", "Blue paint"]
SHOPPINGLIST[1] = "Bottle of Water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
Use the initializer syntax to create an empty array or dictionary.
Let Emptyarray = string[] ()
Let emptydictionary = dictionary<string, float> ()
If the type information is inferred, you can use [] to create an empty array, using [:] To create an empty dictionary, for example, when you set a new value or pass a variable in the method as a parameter
Shoppinglist = []
Control statements
Use if and switch to create a condition that uses for-in,for,while and do-while to create a loop.
The parentheses on the condition and the loop variable are optional. If after curly braces and loop body braces are required.
Let individualscores = [75, 43, 103, 87, 12]
var Teamscore = 0
For score in individualscores{
If score > 50{
Teamscore + = 3
}else{
Teamscore + = 1
}
}
Teamscore
In an If statement, the condition must be an expression of a Boolean type--this is represented as if score {...} This code is wrong, this score is not implicitly to compare with 0.
You can use both if and let when the value may be empty. These values are represented as optional. An optional value may contain a value or contain a Nil,nil indicating that the value is empty. Write a question sign (?) after the type of the value to mark the value as optional.
var optionalstring:string? = "Hello"
Optionalstring = Nil
var optionalname:string? = "John Appleseed"
var greeting = "Hello!"
If let name = optionalname{
Greeting = "Hello, \ (name)"
}
If the optional value is nil, then this condition is false, and the code in the curly braces is skipped. Otherwise, the optional value executes the code in the following code block and assigns the constant after the Let.
Switches supports a variety of data and various comparison operations-they are not limited to integers and equality comparisons.
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."
}
After executing the code in the matching case, the program exits the switch statement. Execution does not continue in the next case, so there is no need to display a break statement that jumps out of the switch at the end of each cases.
You can use the For-in statement to iterate through all items in a dictionary of key-value pairs.
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
Use while to repeat the code in the code block until the condition changes. The loop condition can also be placed at the end to ensure that the loop executes at least once.
var n = 2
While N < 100{
n = n * 2
}
N
var m = 2
do{
m = m * 2
} While M < 100
M
You can keep an index in the loop index--by using: To limit an index range or to indicate an initial value, condition, and increment. The following two results in the same loop:
var firstforloop = 0
For I in 0..3{
Firstforloop + = i
}
Firstforloop
var secondforloop = 0
for var i = 0; I < 3; ++i{
Secondforloop + = 1
}
Secondforloop
Use.. To limit the range will omit the upper value of the range, using ... To limit the scope will contain all the qualified values. (Translator Note: Is left closed right open integer range, ... Is left closed right closed integer interval)