//闭区间运算符
foriin0...2{
print(i)
}
for_in0...1{
print("sb")
}
//半开区间运算符
foriin0..<2{
print(i)
}
//取数组内部几个值
let names = ["张","李","王"]
fornameinnames[0...1] {
print(name)
}
//单侧区间 让区间超一个方向尽可能远
fornameinnames[2...] {
print("单侧区间------\(name)")
}
fornameinnames[...1] {
print("单侧区间~~~~~~\(name)")
}
//带间隔的区间 从4开始 累加2 不超过10
forageinstride(from:4,through:10,by:2) {
print("---带间隔的区间\(age)")
}
/*
switch 注意点
case后不能写大括号
默认不写break 并且不会贯穿下去
必须保证能处理所有情况 否则报错
case 和 default 后面必须有一条语句 如果不处理任何事情就要加上break
如果想贯穿下去加关键字 fallthrough
*/
let number = 1
switch number {
case 1:
print("1")
fallthrough
case 2:
print("2")
default:
break
}
enum Answer {case right , wrong}
letanswer= Answer.right
switch answer {
case .right:
print("right")
case.wrong:
print("wrong")
}
// 复合条件 switch也支持String Character 类型
let str = "left"
switch str {
case "left":
fallthrough
case "right":
print("_______")
default:
break
}
switch str {
case "left","right":
print("等同于上面的写法")
default:
break
}
//区间匹配
let count = 80
switch count {
case 0:
print("")
case1...50:
print("1-50")
case51..<100:
print("51-99")
default:
print(".....")
}
//元组匹配
letpoint= (1,1)
switch point {
case(0,0):
print("原点")
case(_,0):
print("在x轴上")
case(0,_):
print("在Y轴上")
case(-2...2,-2...2):
print("点在-2之2的范围的盒子里")
default:
break
}
//绑定值
switch point {
case(letx ,0):
print("x值是\(x)")
case(0,lety):
print("y值是\(y)")
caselet(x,y):
print("x:\(x) y:\(y)")
}
//添加条件
switch point {
caselet(x,y)wherex == y:
print("x == y)")
caselet(x,y)wherex == -y:
print("x == -y")
caselet(x,y):
print("x:\(x) y:\(y)")
}
// where 过滤不需要的结果 求和
let numbers= [10,-20,100,29,-9,-8]
varsum=0;
fornuminnumberswherenum >0{
sum+=num
}
// 加标签
outer:foriin1...4{
forkin1...4{
ifk ==3{
continueouter
}
ifi==3{
breakouter
}
print("i == \(i) k == \(k)")
}
}