本文是學(xué)習(xí)《The Swift Programming Language》整理的相關(guān)隨筆华坦,基本的語(yǔ)法不作介紹,主要介紹Swift中的一些特性或者與OC差異點(diǎn)金踪。
系列文章:
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中枚舉不會(huì)存在默認(rèn)值
關(guān)聯(lián)值(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中的枚舉可以將每個(gè)枚舉值關(guān)聯(lián)上指定的一些值浊洞,這些值類型也可以不同
例子:
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);
執(zhí)行結(jié)果:
Rectangle -> x:10.0,y:10.0,width:100.0,height:50.0
Circle -> x:10.0,y:10.0,radius:100.0
原始數(shù)值(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中的枚舉的值可以是字符串,字符胡岔,整型等多種數(shù)據(jù)類型
隱式賦值(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中枚舉如果指定了數(shù)據(jù)類型法希,默認(rèn)整型就是0開(kāi)始,字符串就是枚舉的名字
例子:
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);
執(zhí)行結(jié)果:
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中存在遞歸枚舉的寫法,需要使用關(guān)鍵字
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))
執(zhí)行結(jié)果:
18
個(gè)人覺(jué)得上述方式放入枚舉中寫很奇怪靶瘸,覺(jué)得應(yīng)該函數(shù)的方式去實(shí)現(xiàn)才更容易理解铁材,這里只是說(shuō)明了一種用法