//: Playground - noun: a place where people can play
import UIKit
var str = "枚举"
//枚举语法
//使用enum关键词来创建枚举并且把它们的整个定义放在一对大括号内:
enum SomeEnumeration{
// enumeration definition goes here
}
//以下是指南针四个方向的一个例子:
enum CompassPoint {
case North
case South
case East
case West
}
/*
注意:
和 C 和 Objective-C 不同,Swift 的枚举成员在被创建时不会被赋予一个默认的整型值。在上面的CompassPoints例子中,North,South,East和West不会隐式地赋值为了0,1,2和3。相反的,这些不同的枚举成员在CompassPoint的一种显示定义中拥有各自不同的值。
*/
//多个成员值可以出现在同一行上,用逗号隔开:
enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
/*
directionToHead的类型可以在它被CompassPoint的一个可能值初始化时推断出来。一旦directionToHead被声明为一个CompassPoint,你可以使用一个缩写语法(.)将其设置为另一个CompassPoint的值:
*/
var directionToHead = CompassPoint.West ;
//当directionToHead的类型已知时,再次为其赋值可以省略枚举名。使用显式类型的枚举值可以让代码具有更好的可读性。
directionToHead = .East ;
//匹配枚举值和Switch语句
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")
}
//当不需要匹配每个枚举成员的时候,你可以提供一个默认default分支来涵盖所有未明确被提出的枚举成员:
let somePlanet = Planet.Earth
switch somePlanet {
case .Earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
//相关值
/*
“定义一个名为Barcode的枚举类型,它可以是UPCA的一个相关值(Int,Int,Int,Int),或者是QRCode的一个字符串类型(String)相关值。”
这个定义不提供任何Int或String的实际值,它只是定义了,当Barcode常量和变量等于Barcode.UPCA或Barcode.QRCode时,相关值的类型。
*/
enum Barcode
{
case UPCA(Int,Int,Int,Int)
case QRCode(String)
}
var productBarcode = Barcode.UPCA(8, 85909, 51223, 3) ;
productBarcode = .QRCode("ABCDEFGHJMNOP");
//下面是等价的
switch productBarcode {
case .UPCA(let numberSystem, let manufacturer, let product, let check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case .QRCode(let productCode):
print("QR code: \(productCode).")
}
//为了简洁,你可以只放置一个var或者let标注在成员名称前:
switch productBarcode {
case let .UPCA(numberSystem, manufacturer, product, check):
print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
case let .QRCode(productCode):
print("QR code: \(productCode).")
}
//原始值
//一个枚举成员存储 ASCII 码的例子
enum ASCIIControlCharacter: Character{
case Tab = "\t"
case LineFeed = "\n"
case CarriageReturn = "\r"
}
//原始值的隐式赋值
//Plant.Mercury赋了初值1,Planet.Venus会拥有隐式赋值2,依次类推。
enum Planett: Int {
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
//下面的例子是CompassPoint枚举类型的精简版,使用字符串作为初值类型,隐式初始化为咩个方向的名称:
enum CompassPointt: String {
case North, South, East, West
}
//使用枚举成员的rawValue属性可以访问该枚举成员的原始值:
let earthOrder = Planett.Earth.rawValue
let sunsetDirction = CompassPointt.West.rawValue
//递归枚举
/*
递归枚举(recursive enumeration)是一种枚举类型,表示它的枚举中,有一个或多个枚举成员拥有该枚举的其他成员作为相关值。使用递归枚举时,编译器会插入一个中间层。你可以在枚举成员前加上indirect来表示这成员可递归。
**/
//枚举类型存储了简单的算数表达式:
enum ArithmeticExpression {
case Number(Int)
indirect case Addition(ArithmeticExpression, ArithmeticExpression)
indirect case Multiplication(ArithmeticExpression, ArithmeticExpression)
}
//也可以在枚举类型开头加上indirect关键字来表示它的所有成员都是可递归的:
indirect enum ArithmeticExpression1 {
case Number(Int)
case Addition(ArithmeticExpression1, ArithmeticExpression1)
case Multiplication(ArithmeticExpression1, ArithmeticExpression1)
}
//递归函数可以很直观地使用具有递归性质的数据结构
func evaluate(expression:ArithmeticExpression1) ->Int{
switch expression {
case .Number(let value):
return value ;
case .Addition(let left, let right):
return evaluate(left) + evaluate(right) ;
case .Multiplication(let left, let right):
return evaluate(left) * evaluate(right);
}
}
计算 (5 + 4) * 2
let five = ArithmeticExpression1.Number(5) ;
let four = ArithmeticExpression1.Number(4) ;
let sum = ArithmeticExpression1.Addition(five, four) ;
let product = ArithmeticExpression1.Multiplication(sum, ArithmeticExpression1.Number(2));
print(evaluate(product));
Swift-枚举
最新推荐文章于 2023-08-25 17:36:00 发布