swift 学习记录(二)

本文详细介绍了Swift编程语言中switch-case语句的使用,包括语法细节、常见错误及解决方法,通过实例展示了如何正确地在不同情况下执行代码块。

在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

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值