1. Swift语法中 switch与 OC 中的比较
1> 在OC中的switch
- 必须是整数(int)类型,他只能判断整数
- 可以穿透,可以连着写两个case条件语句但是不加break
- 必须要带上break,表示跳出条件语句
- 在case中定义变量要加大括号,保证分隔作用域
- 可以不写default
- default位置可以随便放
2> 在Swift中的switch
- 不需要break,只要符合其中一个case的条件语句,就会跳出swich
- 不局限在对 int 分支,可以对任意数据类型进行检测(它的可以是对象,也可以duoble类型)
- 不能没有default,default位置只能在最后
- 各个cese之间不能穿透,如果有多个值,可以使用","隔开
- 定义变量不需要使用{}分隔作用域
- 在Swift中,必须涵盖所有条件,每个case至少包含一条语句
3> Swift的swich相比于OC更加的强大
它还可以使用在别的上面
- 可以使用区间运算
- 可以用在元组里面
- 值绑定(swich的case可以将匹配到的值绑定为一个临时的常量或者变量,来给case函数体使用)
- 可以使用where分句来检查是否符合特定的约束(根据条件绑定)
- 复合匹配
- 复合值绑定
- fallthrough:如果需要C或者OC一样风格的贯穿行为,可以使用fallthrogh,只要使用了fallthrogh,不会立即跳出switch语句,后面的case条件语句一样会执行
- 语句标签:可以用语句标签来给循环语句或者条件语句做标记.
2.两者区别代码说明
- OC代码
int score = 10;
switch (score) {
case 10:
NSLog(@"优");
break;
case 9:
NSLog(@"良");
break;
case 8:
NSLog(@"一般");
break;
default:
NSLog(@"差");
break;
}
//输出结果:优
//可以穿透
int score = 10; //这个变量只能是整数,不能判断对象等
switch (score) {
case 10:
case 9: //oc中的switch可以穿透,可以连着写两个case条件但是中间不加break
NSLog(@"优良");
break; //必须要带上break,表示跳出条件语句
case 8:
NSLog(@"一般");
break;
default:
NSLog(@"差");
break;
}
//输出结果:优良
//可以不写default
int score = 10;
switch (score) {
case 10:
NSLog(@"优");
break;
case 9:
NSLog(@"良");
break;
case 8:
NSLog(@"一般");
break;
// default:
// NSLog(@"差");
// break;
}
//输出结果: 优
//default的位置可以随便放
int score = 10;
switch (score) {
default:
NSLog(@"差");
break;
case 10:
NSLog(@"优");
break;
case 9:
NSLog(@"良");
break;
case 8:
NSLog(@"一般");
break;
}
//输出结果: 优
//在case中定义变量要加大括号,保证分隔作用域
int score = 9;
switch (score) {
case 10:
NSLog(@"优");
break;
case 9: { //在case中定义变量要加大括号,保证分隔作用域
NSString *name = @"张三";
NSLog(@"%@ 良",name);
break;
}
case 8:
NSLog(@"一般");
break;
default:
NSLog(@"差");
break;
}
//输出结果:
- Swift代码
//不需要break,只要符合其中一个case的条件语句,就会跳出swich
let number = "10" //可以定义任意数据类型
switch number {
case "10":
print("优")
case "9":
print("良")
case "8":
let name = "老王" //这里定义变量可以不用大括号进行分隔
print("\(name)一般")
default:
print("差")
}
输出结果:优
//各个cese之间不能穿透,如果有多个值,可以使用","隔开
let number = "10" //可以定义任意数据类型
switch number {
case "10", "9": //注意OC是不能这样写的(各个cese之间不能穿透,如果有多个值,可以使用","隔开)
print("优")
case "8":
let name = "老王" //这里定义变量可以不用大括号进行分隔
print("\(name)一般")
default:
print("差")
}
//输出结果: 优
//swich也可以使用区间运算
let number = 62
switch number {
case 1..<12:
print("这个数的范围是1到12")
case 12..<100:
print("这个数的范围是12到100")
case 100..<1000:
print("这个数的范围是100到1000")
default:
print("many")
}
//输出结果: 这个数的范围是12到100
//swich也可以用在元组里面,元组和区间并用
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("the point at the origin")
case (1, 0):
print("(\(somePoint.0), 0) is on the x-axis")
case (0, 1):
print("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2): //可以在元组中间再加上区间
print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
print("(\(somePoint.0), \(somePoint.1)) is outside the box")
}
//输出结果:(1, 1) is inside the box
//"_"表明匹配所有可能的值 "_":通配符
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("the point at the origin")
case (_, 0):
print("somePoint is on the x-axis")
case (0, _):
print("somePoint is on the y-axis")
case (-2...2, -2...2):
print("somePoint is inside the box")
default:
print("somePoint is outside the point")
}
//输出结果:(1, 1) is inside the box
//值绑定(swich的case可以将匹配到的值绑定为一个临时的常量或者变量,来给case函数体使用)
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
print("the point at the origin")
case (let x, 0): //会将somePoint中X的值赋值给X
print("(\(somePoint.0), \(somePoint.1)) is on the x-axis, it is value is \(x)")
case (0, let y):
print("on the y-axis with a value is \(y)")
case (let x, let y): // 会将point中XY的值赋值给XY
print("someWhere else at (\(x), \(y)) ")
//default: //如果前面已经有匹配case的值了,没有省略default的话,这里会报一个警告(Default will never be executed,表示这个语句永远不会被执行)所以这里的default可以省略
// print("somePoint is outside the point")
}
//输出结果: someWhere else at (1, 1)
//swich case可以使用where分句来检查是否符合特定的约束(根据条件绑定)
let anotherPoint = (1, -1)
switch (anotherPoint) {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y: //只有where后面的条件表达式为真才赋值并执行case后的语句
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is other point")
}
//输出结果: (1, -1) is on the line x == -y
//复合匹配
let someCharacter: Character = "e"
switch someCharacter {
case "a", "o", "e", "i", "u": //使用“,”分隔多个值
print("\(someCharacter) is a vowel") //vowel:元音
case "b", "c", "d", "f", "g", "h":
print("\(someCharacter) is a consonant") //consonant:辅音
default:
print("\(someCharacter) is not a vowel or a consonant")
}
//输出结果: e is a vowel
//复合值绑定
let anotherPoint = (9, 0)
switch anotherPoint {
case (let distence, 0), (0, let distence): //元组加上值绑定,有多个值,使用了","隔开
print("\(distence) from the origin")
default:
print("no no no")
}
//输出结果: 9 from the origin
//fallthrough:如果需要C或者OC一样风格的贯穿行为,可以使用fallthrogh,只要使用了fallthrogh,不会立即跳出switch语句,后面的case条件语句一样会执行
let someCharacter: Character = "a"
switch someCharacter {
case "A":
print("这个字母是大写的A")
fallthrough
case "a":
print("is the first letter of the alphabet")
fallthrough
case "z":
print("the last letter of the alphabet")
fallthrough
default:
print("no one letter")
}
//输出结果: is the first letter of the alphabet
// the last letter of the alphabet
// no one letter
//语句标签:可以用语句标签来给循环语句或者条件语句做标记.
var number = 10
whileLoop: while number > 0 {
switch number {
case 9:
print("9")
case 10:
var sum = 0
for index in 1...10 {
sum += index
if index == 9 {
print(sum)
break whileLoop
}
}
default:
break
}
number -= 1
}
输出结果:45