1. 语句结尾没有分号;
var x = 0.0, y = 0.0, z = 0.0
2. 变量名 命名规则:Constant and variable names cannot contain mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Nor can they begin with a number, although numbers may be included elsewhere within the name.
3. “NOTE 如果要使用保留符号作为变量名: `变量名
If you need to give a constant or variable the same name as a reserved Swift keyword, you can do so by surrounding the keyword with back ticks (`) when using it as a name. However, you should avoid using keywords as names unless you have absolutely no choice.”
4. 输出
“You can print the current value of a constant or variable with the println function:
println("The current value of friendlyWelcome is \(friendlyWelcome)")
println is a global function that prints a value, followed by a line break, to an appropriate output”
“second function, print, performs the same task without appending a line break to the end of the value to be printed.”
5. 多行注释,可以嵌套 !!
“Unlike multiline comments in C, multiline comments in Swift can be nested inside other multiline comments.”
“/* this is the start of the first multiline comment
/* this is the second, nested multiline comment */
this is the end of the first multiline comment */”
6. Numeric Literals
A decimal number, with no prefix
A binary number, with a 0b prefix
An octal number, with a 0o prefix
A hexadecimal number, with a 0x prefix
All of these integer literals have a decimal value of 17:
let decimalInteger = 17
let binaryInteger = 0b10001 // 17 in binary notation
let octalInteger = 0o21 // 17 in octal notation
let hexadecimalInteger = 0x11 // 17 in hexadecimal notation”
Floating Points
1.25e2 means 1.25 × 102, or 125.0.
1.25e-2 means 1.25 × 10-2, or 0.0125.
0xFp2 means 15 × 22, or 60.0.
0xFp-2 means 15 × 2-2, or 3.75.
为了方便阅读,可以添加下划线和0
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1
7. typealias AudioSample = UInt16
8. Tuples
let http404Error = (404, "Not Found")
let (statusCode, statusMessage) = http404Error
println("The status code is \(statusCode)")
// prints "The status code is 404"
println("The status message is \(statusMessage)")
// prints "The status message is Not Found”
let (justTheStatusCode, _) = http404Error
println("The status code is \(justTheStatusCode)")
// prints "The status code is 404”
Alternatively, access the individual element values in a tuple using index numbers starting at zero:
println("The status code is \(http404Error.0)")
// prints "The status code is 404"
println("The status message is \(http404Error.1)")
// prints "The status message is Not Found"
let http200Status = (statusCode: 200, description: "OK")
If you name the elements in a tuple, you can use the element names to access the values of those elements:
println("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
println("The status message is \(http200Status.description)")
// prints "The status message is OK
9. Optionals
let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber is inferred to be of type "Int?", or "optional Int”
“If Statements and Forced Unwrapping
You can use an if statement to find out whether an optional contains a value. If an optional does have a value, it evaluates to true; if it has no value at all, it evaluates to false.”
“Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. ”
“if convertedNumber {
println("\(possibleNumber) has an integer value of \(convertedNumber!)")
} else {
println("\(possibleNumber) could not be converted to an integer")
}
// prints "123 has an integer value of 123”
10. Optional Binding
“Optional Binding
You use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. ”
“if let constantName = someOptional {
statements
}”
11.
“Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid:
if x = y {
// this is not valid, because x = y does not return a value
}”
12.
“-9 % 4 // equals -1
Inserting -9 and 4 into the equation yields:
-9 = (4 × -2) + -1
giving a remainder value of -1.
The sign of b is ignored for negative values of b. This means that a % b and a % -b always give the same answer.”