八、列挙
1532 ワード
エニュメレート・グループの関連する値として、任意のタイプの関連値がエニュメレート・メンバに格納されることを定義している.
1.定義と作成
1.定義と作成
case , “,”
enum {
case 1
case 2, 3
}
//.Spring spring
enum Seasons {
case Spring,Summer,Sutumn,Sinter
}
let currentSeason : Seasons = Seasons.Summer
print(currentSeason); //summer
2.列挙マッチングswitch currentSeason{
case .Spring:
print("123")
default:
print("this is not Spring").
} //this is not Spring
3.関連値// , case
( let ) ( var ) :
enum Barcode {
case upc(Int,Int,Int,Int)
case qrCode(String)
}
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
switch productBarcode {
case .upc(let num1,let num2,let num3,let num4):
print("upc")
case .qrCode(let num):
print("qrCode")
}
//upc
4.元の値(標準値)enum ASCIIControlCharacter: Character {
case tab = "\t"
case lineFeed = "
"
case carriageReturn = "\r"
}
5.元の値の暗黙的な割り当て , 0; 1 ,
+1;
rawValue
を介して、エニュメレート・メンバの値を取得したり、エニュメレート・メンバにアクセスしたりする.enum tempType :Int{
case table,table2,table3
}
print(tempType.table) //table
print(tempType.table2.rawValue) //1
// ,
let possibleType = tempType(rawValue: 2)
6.再帰的な列挙:関連値として、エニュメレート・タイプの例を使用する1つ以上のエニュメレート・メンバがいる.