文档地址:
1、for _ in 1…9 {
}
for this calculation,the individual counter values each time(这里指的是1到9的数) through are unnecessary
于是就写成这样
2、let numberOfLegs = [“spider”:8,”ant”:6,”cat”:4]
for (animalName,legCount) in numberOfLegs {
print (“\(animalName) s have \(legCount) legs”)
}
you can decompose the (key,value) tuple’s members as explicitly names
constants for use within the body of the for-in loop
你能分解一个元组as清晰名字常量
3、let names = [“Anna”,”Alex”,”Brian”,”jack”]
for name in names {
print(“Hello,\(name)!”)
}
这种写法也可以
4、A while loop performs a set of statements until a condition becomes false.These kinds of loops are best used when the number of iterations is not known before the first iteration begins.
while 一直执行{}里面的代码形成一个集合,直到while里面的条件是错的。这种循环最长用在在第一次迭代之前,迭代的数目不知道。
这个跟
while (statement) {
}
5、讲解while时,文章举了一个蛇和梯子的例子。
6、repeat {
statement
}while (statemnet)
这个跟
while(statement) {
}不一致的地方在于这个条件的判断的位置。repeat不管条件成不成立都会执行一次。
7、switch some value to consider {
- casevalue 1:
- respond to value 1
- casevalue 2,
- value 3:
- respond to value 2 or 3
- default:
- otherwise,do something else
- }
默认的是case1,2之外的其余类型。
8、
In contrast withswitch statements in C and Objective-C,switch statements in Swift do not fall through the bottom of each case and into the next one by default. Instead,the entireswitch statement finishes its execution as soon as the first matchingswitch case is completed,without requiring an explicitbreakstatement. This makes the switch statement safer and easier to use than the one in C and avoids executing more than oneswitch case by mistake.
与c和oc不同的是,在swift里面的switch 声明,不会穿过第一个case 而进入下一个case ,整个swift语句只要第一个case完成了,那么整个switch就完成了它的执行,不需要一个明显的关键字break.这意味着switch语句c里面的switch语句更加安全,它能避免错误地执行超过一次case.
这样又让swift产生不同。
swift allows multiple switch cases to consider the same value or values.
swift允许多个switchcase包含多个相同的值。因为反正只执行一个
9、The body of each case must contain at least one executable statement.
每个case的body都必须至少包含一个执行的语句。
10、Unlike a Switch statement in C,this switch statement does not match both “a” and “A”.
不像c,这个switch声明的a不一起匹配a和A。
11、Compound Cased复合的例子
let anotherCharacter: Character = “a”
switch anotherCharacter {
case “a”,“A”:
print(“The letter A”)
default:
print(“Not the letter A)
}
If any of the patterns match,then the case is considered to match.
只要一个pattern匹配,就考虑匹配这个case
The patterns(模式) can be written over multiple lines if the list is long.
写成多条线。
12、 let somePoint = (1,1)
switch somePoint {
case (0,0):
print("(0,0) is at the origin")
case (_,0):
print("\(somePoint.0) is on the x-axis")
case (0,_):
print("(\(somePoint.0),\(somePoint.1) is inside the Box)")
case(-2...2,-2...2):
print("(\(somePoint.0),\(somePoint.1)) is inside the Box")
default:
print("(\(somePoint.0),\(somePoint.1) is outSide of the Box)")
}
判断tupe可以这么写,注意case(_,0),case(-2…2,-2…2)
13、A switch case can bind the value or values it matches to temporary constants or variables,for use in the body of the case.
一个switch case能绑定匹配临时变量或者常量的值,这些值能在case中使用.
let anotherPoit = (2,0)
switch anotherNumber {
case (let x,0):
print("on the x-axis with an x value of\(x)")
case (0,let y):
print("on the y-axis with a y value of\(y)")
caselet (x,y):
print("someWhere else at (\(x),\(y))")
}
//result:Prints "on the x-axis with an x value of 2"
注意:(1)其实第三个case也满足,但是没有打印,原因就是因为只会执行一次便会跳出switch
(2)这里没有default,以为let(x,y)包含了所有的值,这里就不需要声明一个默认值了。
14、A switch case can use a where clause to check for additional conditions.
可以用where关键字
let yetAnotherPoint = (1,-1)
switch yetAnotherPoint {
caselet (x,y) where x == y:
print("\(x),\(y) is on the line x == y")
caselet (x,y) where x == -y:
print("(\(x),\(y) is on the line x == -y)")
caselet (x,y):
print("\(x),\(y) is just some arbitrary point")
}
//Prints”(1,-1) is on the line x == -y”
这里的case 相当于给出了一个范围。
15、Compound cases also include value bindings.
符合case也包括值的捆绑。
let stillAnoterPoitn = (9,0)
switch stillAnoterPoitn {
case (let distance,0),(0,let distance):
print("On an axis,\(distance) from the origin")
default:
print("Not on axis")
}
//注意要有相同的distance
16、Control Transfer Statements
Control transfer statements change the order in which your code is executed .控制传输声明改变你的代码执行顺序
continue,break,fallthrough,return,throw.
The continue,break,andfallthrough statements are described below. Thereturn statement is described inFunctions,and the throw statement is described inPropagating Errors Using Throwing Functions.
17、The continue statement tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop.
继续这个声明告诉循环停止它正在做的什么,然后开始再次进行下一次的循环。
18、break
break in a loop statement
When used inside a loop statement,break ends the loop’s execution immediately and transfers control to the code after the loop’s closing brace(}).
直接跳出循环
break in a Switch Statement 也一样,跳出循环。
The behavior can be used to match and ignore one or more cases in a swich statement.
这个行为能被用来匹配和忽视一个或者更多例子。
19、Aswitch case that contains only a comment is reported as a compile-time error. Comments are not statements and do not cause aswitch case to be ignored. Always use abreak statement to ignore a switch case.
怎么理解。
20、fallthrough
If you need C-style fallthrough behavior,you can opt in(选择参与)to this behavior on a case-by-case basis with the fallthrough keyword.
let integerToDescribel = 5
var description = “The number \(integerToDescribe) is”
switch ingegerToDescribe {
case 2,3,5,7,11,13,17,19:
description += “ a prime number,and also”
fallthrough
default:
description += “an integer”
}
21、Labeled Statements
声明形式
label name: while condition {
statements
}
具体举例:
gameLoop: while square != finalSquare {
diceRoll += 1
if diceRoll == 7 {diceRoll = 1}
switch square + diceRoll {
case finalSquere:
break gameLoop
case let newSquare where newSquare > finalSquare:
continue gameLoop
default:
square += diceRoll
square += board[square]
}
22、guard let name = person[“name”] else {
return
}
Unlike an if statement,a guard statement always has an else clause ——the code inside the else clause is executed if the conditon is not true
如果条件不满足,就执行else里面的,如果满足,就执行后面的代码
guard声明就像一个if 声明。
Checking API Availability
if #available (iOS 10,macOS 10.12,*) {
}else {
}
原文链接:https://www.f2er.com/swift/322020.html