Swift的enum枚举类型介绍

声明枚举类型的几种方法和使用:

一:

enum Direction{
    case North
    case South
    case East
    case West
};
//简写如下
enum Direction2{
    case North,South,West,East
};
二:
//申明一个枚举
var d = Direction.North;
var d2 = Direction.East;
d2 = .South;//.South因为d2肯定是Direction枚举类型
//.South = Direction.South

//明确规定d3类型是Direction,.East就可以省略,同上远离
var d3 : Direction = .East;
var d4 : Direction = Direction.East;

switch d4 {
case .North:
    println("in north")
case .South:
    println("in south")
case .East:
    println("in east")
default:
    println("other")
}

//枚举类型一种,相似C
enum Couse : Int {
    case android = 1, ios, windowsphone, symbian
}

var c : Couse = Couse.ios;
let v = c.rawValue;//ios转换成int
println("v is: \(v)");

var c2 = Couse(rawValue: 4)//根据索引返回枚举值
if c2 == Couse.symbian{
    println("is symbian");
}

你可能感兴趣的:(Swift的enum枚举类型介绍)