print("流程控制")
//if-else
//可以省略()的书写,但是{}不得省略,即使只有一行代码
//条件只能是Bool类型的表达式
if "xxx.zip".hasSuffix("zip") {
print("是压缩文件")
}
//repeat-while,等价于do-while
var i = 3
repeat {
//i--
i -= 1
} while i>0
//for-in
// 1...3 => [1,3]
for i in 1...3 {
print(i)
}
// 1..<3 => [1,3)
for i in 1..<3 {
print(i)
}
//遍历字符串
for i in "abcd".characters {
print(i)
}
//标签使用
for1: for i in 0...5 {
for2: for j in 0...5 {
print("(\(i),\(j))")
#if false
if i+j == 5 {
//结束指定的循环
break for1
}
#else
if i*j == 3{
//继续指定的循环
continue for1
}
#endif
}
}
/*switch-case
1、case后可以匹配任何选项
2、每个case后至少有一条语句,且不需要break
3、若不能罗列所有可能,则必须有default
*/
var num = 2
switch num {
case 1:
print("one")
case 2:
print("two")
default:
print("other")
}
var point = (0.0,1.0)
switch point {
//普通匹配
case (0.0,0.0):
print("原点")
//忽略匹配
case (_,0.0):
print("X轴上")
//where条件
case let(x,y) where x == y:
print("x == y")
//多项匹配
case (_,0.0),(0.0,_):
print("坐标轴上")
fallthrough //穿越过去
//区间匹配
case (0.0...2.0,_):
print("0.0 <= X <= 2.0")
//fallthrough //不能穿越后面的数据绑定
//数据绑定
case let (0,y):
print("y:\(y)")
default:
print("其它")
}
原文链接:https://www.f2er.com/swift/321710.html