最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中switch和case中多个case放在一起以及类似于continue如何写

Swift crifan 5040浏览 0评论

swift中的switch case语句中,多个case放在一起的写法,不是:

switch xxx{
case 1:
case 2:
  yyyy
}
官网的:
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a":
case "A":
    print("The letter A")
default:
    print("Not the letter A")
}
// this will report a compile-time error
搜:
swift for in  range
参考官网:
而是:
switch some value to consider {
case value 1, value 2:
    statements
}
而类似于continue,则用fallthrough:
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number, and also"
    fallthrough
default:
    description += " an integer."
}
print(description)
// prints "The number 5 is a prime number, and also an integer."

 

[总结]

swift中switch的多个case在一起,则用逗号分隔开即可;

而如果想要实现类似于continue的效果,则加上fallthrough。

转载请注明:在路上 » [已解决]swift中switch和case中多个case放在一起以及类似于continue如何写

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.169 seconds, using 22.08MB memory