本文是學習《The Swift Programming Language》整理的相關隨筆,基本的語法不作介紹,主要介紹Swift中的一些特性或者與OC差異點。
系列文章:
Enumerations
You introduce enumerations with the enum keyword and place
their entire definition within a pair of braces:
enum SomeEnumeration {
// enumeration definition goes here
}
例子:
enum CompassPoint {
case north
case south
case east
case west
}
Unlike C and Objective-C, Swift enumeration cases are not
assigned a default integer value when they are created.
- Swift中枚舉不會存在默認值
關聯值(Associated Values)
You can define Swift enumerations to store associated
values of any given type, and the value types can be
different for each case of the enumeration if needed.
Enumerations similar to these are known as discriminated
unions, tagged unions, or variants in other programming
languages.
- Swift中的枚舉可以將每個枚舉值關聯上指定的一些值,這些值類型也可以不同
例子:
enum Shape{
case Rectangle(x:Float,y:Float,width:Float,height:Float)
case Circle((x:Float,y:Float),radius:Float)
}
func judgeShape(_ shape:Shape){
switch shape {
case Shape.Rectangle(let x,let y,let width,let height):
print("Rectangle -> x:\(x),y:\(y),width:\(width),height:\(height)");
case Shape.Circle((let x,let y), let radius):
print("Circle -> x:\(x),y:\(y),radius:\(radius)");
}
}
var rectangle = Shape.Rectangle(x: 10, y: 10, width: 100, height: 50);
var circle = Shape.Circle((x: 10, y: 10), radius: 100);
judgeShape(rectangle);
judgeShape(circle);
執行結果:
Rectangle -> x:10.0,y:10.0,width:100.0,height:50.0
Circle -> x:10.0,y:10.0,radius:100.0
原始數值(Raw Values)
Raw values can be strings, characters, or any of the
integer or floating-point number types. Each raw value
must be unique within its enumeration declaration.
- Swift中的枚舉的值可以是字符串,字符,整型等多種數據類型
隱式賦值(Implicitly Assigned Raw Values)
For instance, when integers are used for raw values, the
implicit value for each case is one more than the previous
case. If the first case doesn’t have a value set, its
value is 0.
When strings are used for raw values, the implicit value
for each case is the text of that case’s name.
- Swift中枚舉如果指定了數據類型,默認整型就是0開始,字符串就是枚舉的名字
例子:
enum CompassPoint: String{
case north, south, east, west
}
print(CompassPoint.north.rawValue);
enum Planet: Int{
case mercury = 0, venus, earth, mars, jupiter, saturn, uranus, neptune
}
print(Planet.mercury.rawValue);
執行結果:
north
0
遞歸枚舉(Recursive Enumerations)
A recursive enumeration is an enumeration that has another
instance of the enumeration as the associated value for
one or more of the enumeration cases. You indicate that an
enumeration case is recursive by writing indirect before
it, which tells the compiler to insert the necessary layer
of indirection.
- Swift中存在遞歸枚舉的寫法,需要使用關鍵字
indirect
修飾它
例子:
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
print(evaluate(product))
執行結果:
18
個人覺得上述方式放入枚舉中寫很奇怪,覺得應該函數的方式去實現才更容易理解,這里只是說明了一種用法