感谢作者
https://www.jianshu.com/p/0a8fbf324a90
enum CompassPoint: String, CaseIterable {
case north
case west
case east
case south
// 使用mutating关键字放在枚举或结构体中所定义方法的func关键字之前,
// 使得该方法可以在方法中修改枚举或结构体的属性。
mutating func turnNorth() -> Void {
self = .north
}
}
//多个枚举写在一行中。
//enum CompassPoint {
// case north,west,east,south
//}
enum ProductCode {
case upc(Int,Int,Int,Int)//通用商品条码
case qrCode(String)//二维码
}
enum NameType : String {
case ZhangFei = "张飞"
case GuanYu = "关羽"
case LiuBei = "刘备"
}
enum Planet: Int,CaseIterable {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
}
//方式一
//递归枚举,存储了三个数学表达式 使用`indirect`来表示枚举项时可递归调用的。
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
//方式二
indirect enum ArithmeticExpression2 {
case number(Int)
case addition(ArithmeticExpression2, ArithmeticExpression2)
case multiplication(ArithmeticExpression2, ArithmeticExpression2)
}
// Enumeration:枚举类型
// enum <#name#> {
// case <#case#>
// }
var direction1 : CompassPoint = .north;
var direction2 = CompassPoint.north;
switch direction1 {
case .east:
print("ease");
case .north:
print("north");
case .west:
print("west");
case .south:
print("south");
}
// 迭代枚举项
print(CompassPoint.allCases);
switch CompassPoint.allCases.first! {
case .north:
print("输出正确");
default:
print("输出失败");
}
//eachCase每一项都是枚举类型的示例
for eachCase in CompassPoint.allCases {
/*north
west
east
south*/
print(eachCase);
}
// 枚举关联值
var productCode = ProductCode.upc(1, 22, 333, 4444);
switch productCode {
case .upc(let x, let y, let z, let zz):
print("从switch语句中提取的相匹配的枚举项的关联值为x:\(x) y:\(y) z:\(z) zz:\(zz)");
case ProductCode.qrCode(let str):
print("从switch语句中提取的相匹配的枚举项的关联值为:\(str)");
}
productCode = ProductCode.qrCode("二维码哈")
switch productCode {
case let .upc(x, y, z, zz):
print("从switch语句中提取的相匹配的枚举项的关联值为x:\(x) y:\(y) z:\(z) zz:\(zz)");
case let .qrCode(str):
print("从switch语句中提取的相匹配的枚举项的关联值为:\(str)");
}
// 枚举原始值
let namee = NameType.ZhangFei;
print(namee);//!< log:张飞
print(namee.rawValue);//!< log:张飞
// 原始值与关联值不同。
// 原始值:预先填充的默认值,对于特定的枚举项,其原始值总是相同的。
// 关联值:每次基于枚举情况之一创建一个常量或变量时,其关联值可以是不同的。
// 隐式分配原始值
//查看各项的`rawvalue`
var planetOutStr = "枚举类型Planet各项的原始值";
for item in Planet.allCases {
planetOutStr += "\(item) : \(item.rawValue) ";
}
print(planetOutStr);
//!< 枚举类型Planet各项的原始值mercury : 1 venus : 2 earth : 3 mars : 4 jupiter : 5 saturn : 6 uranus : 7 neptune : 8
var directionOutStr = "枚举类型CompassPoint各项的原始值";
for item in CompassPoint.allCases {
directionOutStr += "\(item) : '\(item.rawValue)' ";
}
print(directionOutStr);
//!< 枚举类型CompassPoint各项的原始值north : 'north' west : 'west' east : 'east' south : 'south'
// 从原始值初始化
let possiblePalnet = Planet.init(rawValue: 7);
//!< Planet类型的可选项。Optional(swift_basic.EnumerationPractice.Planet.uranus)
//无法根据原始值匹配到枚举项的情况
let possiblePalnet2 = Planet.init(rawValue: 9);
//!< Planet类型的可选项。9已经超出了枚举的所有项的范围。log:nil
//需要解包,可选绑定
if let possiblePlanet = possiblePalnet2 {
print(possiblePlanet);
switch possiblePlanet {
case .earth:
print("地球");
default:
print("其他行星");
}
} else {
print("没有找到合适的枚举项");
}
// 枚举的递归
// 定义一个枚举时,若该枚举类型的某个case关联了一个或多个该枚举类型的值时,
// 系统会自动提示,需要添加indirect关键字,因为此时的枚举类型已经被定义为了
// 拥有递归结构的递归枚举。
//存储了一个为5的关联值
let five = ArithmeticExpression.number(5);
//存储了一个为6的关联值
let six = ArithmeticExpression.number(6);
//存储了一个为3的关联值
let threee = ArithmeticExpression.number(3);
//addition枚举项,关联了两个当前枚举类型的值。
let sum = ArithmeticExpression.addition(five, six);
//multiplication枚举项,关联了两个当前枚举类型的值。
let multiplication = ArithmeticExpression.multiplication(sum, threee);
//数学表达式的呈现形式。
print("嵌套的数学表达式进行递归调用后的结果是:");
print(arithmeticOperation(multiplication));
//!< log:嵌套的数学表达式进行递归调用后的结果是:33
// 创建一个递归函数验证可递归性
func arithmeticOperation(_ expression : ArithmeticExpression) -> Int {
switch expression {
case let .number(x):
return x
case let .addition(x, y):
//! 此处x和y仍旧是表达式,所以需要先进行递归运算得出`Int`类型的关联值
return arithmeticOperation(x) + arithmeticOperation(y)
case .multiplication(let x, let y):
//! 此处x和y仍旧是`ArithmeticExpression`,所以需要先进行递归运算得出`Int`类型的关联值
return arithmeticOperation(x) * arithmeticOperation(y)
}
}
网友评论