Swift-控制符

文章是根据The Swift Programming Language 来总结的,今天就学习一下最基础的控制符,本文总结的是ifswitchfor-inforwhilerepeat-while

首先注意的是:条件语句括号()可以省略,跟其他编程语言有很大的差别,但是语句体的大括号{}是必须的,下面会有demo展示

if

基本语法

1
2
3
4
5
OC: 
Bool flag = YES;
if (flag) {
// TODO
}
1
2
3
4
5
6
7
8
Swift:
let flag = true
if flag {
// TODO
}
if (flag) {
// TODO
}

区别

在Swift中,条件必须是一个布尔表达式,也就是说不是true就是false
但是在OC中,什么表达式都可以,前提是条件语句成立,甚至是一个对象

1
2
3
4
5
6
7
8
9
OC:
NSString *str = @"条件";
if (str) {
// TODO
}
NSInteger age = 10;
if (age) {
// TODO
}

但是在Swift不行,一下是错误写法,会报出:

Type ‘String’ does not coonform to protocol ‘BooleanType’

Type ‘Int’ does not coonform to protocol ‘BooleanType’

1
2
3
4
5
6
7
8
9
Swift:
var name = "条件"
if name {
// TODO
}
let age = 10
if age {
// TODO
}

正确的写法是:

1
2
3
4
5
6
7
8
9
10
Swift:
var name : String? = "条件"
if let newName = name {
// TODO
print(newName); // 打印条件
}
let age = 10
if age > 5 {
// TODO
}

这里可能涉及一点其他知识,就是条件语句使用字符串,不敢保证是不为nil的,所以需要用let处理字符串确实,就是字符串为nil,用到可选类型,就是在声明变量类型后面加上一个问号

1
var name : String? = "条件"

这句就是表示,name可以有值,也可以为nil,是可选类型
搭配if来用

1
2
3
4
if let newName = name  {
// TODO
print(newName); // 打印条件
}

如果name是nil,为false,那么就不会条件代码块,当name不为nil的时候,赋值给newName,条件成立,为true,执行大括号的代码块。当然也可以这样写

1
2
3
4
if let newName: String? = "字符串"  {
// TODO
print(newName); // 打印条件
}

switch

switch没什么值得讲的,用发跟OC一样,但是条件类型就不是一样了,Java6以上是支持字符串的,Swift也是一样,格式是:
格式: switch(需要匹配的值) case 匹配的值: 需要执行的语句 break;而且条件 支持任意类型的数据以及各种比较操作

OC与Swift的区别:

  • OC可以不写default,default位置可以随便写
1
2
3
4
5
6
7
8
9
10
11
12
OC:
NSInteger age = 10;
switch (age) {
case 1:
{
break;
}
case 2:
{
break;
}
}
  • Swift一定要写default,default位置只能在最后
1
2
3
4
5
6
7
8
Swift:
let age = 10
switch age {
case 1:
print("ahah")
default:
print("haha")
}
  • OC不能判断对象,必须是整数
1
2
3
4
5
6
7
8
9
10
11
12
OC:
NSNumber *age = @10;
switch (age) {
case @1:
{
break;
}
case @2:
{
break;
}
}
  • Swift可以判断对象
1
2
3
4
5
6
7
8
9
10
Swift:
var name = "daisuke"
switch name {
case "daisuke":
print("ahah")
case "alex":
print("ahah")
default:
print("haha")
}
  • OC可以穿透
1
2
3
4
5
6
7
8
9
10
OC:
NSInteger age = 10;
switch (age) {
case 1:
case 2:
case 3:
{
break;
}
}
  • Swift不可以穿透,可以不写break
1
2
3
4
5
6
7
8
9
10
11
12
Swift:
let age = 10
switch age {
case 1: // 相当于 if
print("ahah")
case 2: // 相当于 else if
print("ahah")
case 3: // 相当于 else if
print("ahah")
default:
print("haha")
}
1
2
3
4
5
6
7
8
9
10
11
// 不可以这样写,因为不能穿透
Swift:
let age = 10
switch age {
case 1:
case 2:
case 3:
print("ahah")
default:
print("haha")
}
1
2
3
4
5
6
7
8
9
10
// 如果一定要跳过某个条件的话,可以这样写,但是OC绝对不可以
Swift:
let age = 10
switch age {
case 1 , 2:
case 3:
print("ahah")
default:
print("haha")
}
  • OC在case中定义变量或者其他语句,一定要花括号{},不然作用域混乱
1
2
3
4
5
6
7
8
9
10
11
12
13
14
OC:
NSInteger age = 10;
switch (age) {
case 1:
{
NSInteger height = 170;
break;
}
case 2:
case 3:
{
break;
}
}
  • Swift在case中定义变量或者其他语句,不需要花括号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Swift:
let age = 10
switch age {
case 1: // 相当于 if
var name = "daisuke"
var height = 170
print("ahah")
case 2: // 相当于 else if
print("ahah")
case 3: // 相当于 else if
print("ahah")
default:
print("haha")
}

for

  • 在OC中的两种for循环方式

  • 格式: for (初始化表达式;循环保持条件;循环后表达式) {需要执行的语句}

1
2
3
4
for (int index = 0; index < 10; index++) {
// TODO
}
// 其中括号里面的条件`样式`想怎么搞就怎么搞
  • 在Swift的for循环方式
  • 1、for后面的圆括号可以省略
  • 2、只能是bool类型作为条件语句
  • 3、如果只有一条执行代码,for后面的花括号不可以省略
  • 4、for后面的三个参数都可以省略

  • 根据下标实现循环

1
2
3
4
5
var sum:Int = 0;
for var index = 0; index<10; index++
{
sum += index
}

for in

格式: for (接收参数 in 取出的参数) {需要执行的语句}
for in含义: 从(in)取出什么给什么, 直到取完为止
for in 一般用于遍历区间或者集合

1
2
3
4
5
6
7
8
9
10
11
12
13
NSArray *arrary = @[@{@"key":@"value"}, @{@"key1":@"value1"}];
for (NSDictionary *dict in arrary) {
NSLog(@"%@", dict[@"key"]);
}
```

```swift
var sum:Int = 0;
for var index in 1...10
{
// 1...10,表示1到10这个十个数,会将区间的值依次赋值给index
sum += index
}
1
2
3
4
5
var dict = ["name":"daisuke", "age":10]
for (key, value) in dict
{
print("\(key) = \(value)")
}

while

  • OC格式:while(循环保持条件){需要执行的语句}
1
2
3
4
5
6
7
int index = 0;
int sum = 0;
while(index < 10)
{
index++;
sum += index;
}
1
2
3
4
5
int index = 0;
int sum = 0;
while(index < 10)
sum += index++;
// 只有一条执行语句的时候。花括号可以省略
  • Swift格式:while后的圆括号可以省略,只能以bool作为条件语句,如果只有一条指令while后面的大括号不可以省略
1
2
3
4
5
6
7
var index = 0
var sum = 0
while index < 10
{
index++
sum += index
}
1
2
3
4
5
6
7
var index = 0
var sum = 0
while index < 10
{
sum += index++
}
// 只有一条执行语句的时候。花括号不可以省略

repeat while

  • OC中do while循环,格式:do while(循环保持条件) {需要执行的语句}
1
2
3
4
5
6
7
int index = 0;
int sum = 0;
do {
sum += index;
index++;
}
while(index < 10);
  • 在Swift2.0之后改为 repeat while, do用于捕捉异常。格式:while后面的圆括号可以省略,只能bool作为条件语句,只有一条语句的时候,do后面的花括号不能省略
1
2
3
4
5
6
7
var index = 0
var sum = 0
repeat{
sum += index
index++
}
while index < 10
1
2
3
4
5
6
var index = 0
var sum = 0
repeat{
sum += index++
}
while index < 10