swift枚举

以下是指南针四个方向的一个例子: 
enum CompassPoint {
case North
case South
case East
case West
 
多个成员值可以出现在同一行上,用逗号隔开:

enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune

匹配枚举值和 语句 你可以使用 语句匹配单个枚举值:

Compass

directionToHead
directionToHead = .South switch directionToHead { case .North:

print("Lots of planets have a north") case .South: 

print("Watch out for penguins") case .East:

print("Where the sun rises") case .West:

print("Where the skies are blue") }

// 输出 "Watch out for penguins” 

 

当不需要匹配每个枚举成员的时候,你可以提供一个默认

let somePlanet = Planet.Earth switch somePlanet {
case .Earth:

print("Mostly harmless") default:

print("Not a safe place for humans") }

// 输出 "Mostly harmless” 

转载于:https://www.cnblogs.com/supersr/p/5416693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值