swift 学习记录(二)
2015-10-16 14:05
471 查看
在switch case中,每个case分支的statements不需要用{ }包含,例如
(1)
let oneSring: String = "abc"
switch oneSring {
case "abc":
{
print("The String is oneSring")
let bbb = 100
print(bbb)
print("The String is qqq")
print("The String is www")
print("The String is eee")
print("The String is sss")
print("The String is DDD")
}
case "A":
print("The String is A")
default:
print("Not find String")
}
在第一个case分支,会报错:
<stdin>:17:1: error: braced block of statements is an unused closure
{
^
<stdin>:17:1: error: expression resolves to an unused function
{
^
(2)每个case分支如果有多条语句,直接换行写就可以了,
let oneSring: String = "abc"
switch oneSring {
case "abc":
print("The String is oneSring")
let bbb = 100
print(bbb)
print("The String is qqq")
print("The String is www")
print("The String is eee")
print("The String is sss")
print("The String is DDD")
case "A":
print("The String is A")
default:
print("Not find String")
}
输出结果为:
The String is oneSring
100
The String is qqq
The String is www
The String is eee
The String is sss
The String is DDD
(1)
let oneSring: String = "abc"
switch oneSring {
case "abc":
{
print("The String is oneSring")
let bbb = 100
print(bbb)
print("The String is qqq")
print("The String is www")
print("The String is eee")
print("The String is sss")
print("The String is DDD")
}
case "A":
print("The String is A")
default:
print("Not find String")
}
在第一个case分支,会报错:
<stdin>:17:1: error: braced block of statements is an unused closure
{
^
<stdin>:17:1: error: expression resolves to an unused function
{
^
(2)每个case分支如果有多条语句,直接换行写就可以了,
let oneSring: String = "abc"
switch oneSring {
case "abc":
print("The String is oneSring")
let bbb = 100
print(bbb)
print("The String is qqq")
print("The String is www")
print("The String is eee")
print("The String is sss")
print("The String is DDD")
case "A":
print("The String is A")
default:
print("Not find String")
}
输出结果为:
The String is oneSring
100
The String is qqq
The String is www
The String is eee
The String is sss
The String is DDD
相关文章推荐
- swift反射──Mirror(swift 2.0)
- 通过运行时给view关联一个nameTag(含Swift2.0)
- Swift2.1 语法指南——泛型
- swift 替换反斜杠
- 《从零开始学Swift》学习笔记(Day 23)——尾随闭包
- Swift UITableView相关功能(一)创建tableView
- 《从零开始学Swift》学习笔记(Day 23)——尾随闭包
- Swift2.1 语法指南——协议
- 学习 swift (1)
- Swift & the Objective-C Runtime
- swift 2.0 函数
- swift 2.0 循环语句
- Swift基础知识_01
- Swift利用inout交换两个数的位置
- swift 2.0 分支语句 if...else & switch
- swift 2.0 元组
- swift 2.0 字典
- swift 2.0 数组
- swift 2.0 基本数据类型 强转类型
- Swift 2.0 手动添加数据类型的标注