Swift 学习笔记 enum 枚举类型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift 学习笔记 enum 枚举类型相关的知识,希望对你有一定的参考价值。
异端,异端啊,我感觉这是map吧?
1 enum ATMStatus { 2 case Success(Int) 3 case Error(String) 4 } 5 6 var balance = 200 7 8 func withdraw(amount: Int) -> ATMStatus { 9 if balance >= amount { 10 balance -= amount 11 return .Success(balance) 12 } else { 13 return .Error("QiongB") 14 } 15 } 16 17 let result = withdraw(30) 18 19 switch result { 20 case .Success: 21 //print(money) 22 print("I am here") 23 break 24 case let .Error(msg): 25 print(msg) 26 break 27 }
还可以递归包含,但是声明的enum前面要加indirect
1 indirect enum Expression { 2 case Number(Int) 3 case Addition(Expression, Expression) 4 case Multiply(Expression, Expression) 5 } 6 7 //(5 + 4) * 2 8 let five = Expression.Number(5) 9 let four = Expression.Number(4) 10 let sum = Expression.Addition(five, four) 11 let multi = Expression.Multiply(sum, four) 12 13 func compute(expression: Expression) -> Int { 14 switch expression { 15 case let .Number(value): 16 return value 17 case let .Addition(left, right): 18 return compute(left) + compute(right) 19 case let .Multiply(left, right): 20 return compute(left) * compute(right) 21 } 22 } 23 24 compute(multi)
学习进度缓慢,咸鱼
以上是关于Swift 学习笔记 enum 枚举类型的主要内容,如果未能解决你的问题,请参考以下文章