Swift的enum枚举类型介绍

前端之家收集整理的这篇文章主要介绍了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相关文章