New features in the Language Foundation
names of constants and variables
Swift can use almost any character as a constant and variable name, including Unicode:
let π = 3.14159let 你好 = "你好世界"let 星星 = "★"
annotations can be nested
Multiple lines of comments in Swift /**/ can be nested, which makes it easy to add comments in a large piece of commented code block:
/* 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 */
Quantity Representation
Floating point 12.1875 can be expressed as:
let decimalDouble= 12.1875let exponentDouble= 1.21875e1
Numbers can contain additional underscores _ , which increase the readability of the code, but do not affect the operation of the code:
let paddedDouble= 000123.456let oneMillion= 1_000_000let justOverOneMillion= 1_000_000.000_000_1
Tuple Type
You can define a tuple type in swift, encapsulate some different data into an element, and also return as a function value:
let (statusCode, statusMessage) = http404Errorprintln("The status code is \(statusCode)")// prints "The status code is 404"println("The status message is \(statusMessage)")// prints "The status message is Not Found"
If you only need individual values in tuples, you can use _ them to ignore other values:
let (justTheStatusCode, _) = http404Errorprintln("The status code is \(justTheStatusCode)")// prints "The status code is 404"
Assignment of tuples:
let (x, y) = (1, 2)// x等于1, 并且y等于2
Optional type (Optional type)
In simple terms, this type can be a specific value, or be empty. Use 类型+? the form to define:
let possibleNumber = "123"let convertedNumber = possibleNumber.toInt()// convertedNumber is inferred to be of type "Int?", or "optional Int"
For more information on optional types ? and ! questions, refer to: Question mark in swift and exclamation point!
remainder operation of floating-point number
Floating-point numbers in Swift can also perform remainder operations:
8 % 2.5 // equals 0.5
range operator
Use to a...b define an enclosing range, from A to B including all values A and B
for index in 1...5 {println("\(index) times 5 is \(index * 5)")}// 1 times 5 is 5// 2 times 5 is 10// 3 times 5 is 15// 4 times 5 is 20// 5 times 5 is 25
Use a..b define left closed right open interval, from A to B, including a but not all values of B
Let names= ["Anna.", "Alex", "Brian", "Jack"] LetCount= names. Countfor i in 0.. count {println("person \ (i + 1) are called \ (Names[i])")}/person 1 is called Anna //person 2 was called Alex//person 3 was called Brian//person 4 is called Jack
Switch Statement
Statements in Swift switch do not case automatically jump to the next after a statement because they are not breakcase
iOS Development--swift Chapter &swift new Features (i) Language basics