Swift–09枚举类型

释放双眼,带上耳机,听听看~!
//声明枚举类型

enum Direction {

#iffalse

    case north

    case south

    case east

    case west

#else

    case north,south,east,west

#endif

}




//初始化未指明类型时需要:类型.枚举值

var dir = Direction.east

//当变量类型已知时无需写类型:.枚举值

dir = .east//东

var dir2: Direction = .south




switchdir {

case .north:

    print("北")

case .south:

    print("南")

case .east:

    print("东")

case .west:

    print("西")

}




//原始值

enum Week: Int {

    case monday =
100,tuesday,wednesday,thursday,friday,saturday,sunday

}




//在没有指定枚举值使用的数据类型时,无法获取原始值

print(Week.friday.rawValue)//104




//将一个原始值转换枚举变量,会得到一个可选类型的变量(因为可能会失败)

let dayOne = Week(rawValue:
102)

iflet day =
dayOne {

    print(day)//wednesday

}




//枚举关联值

enum Point {

    case start(x:
Double, y: Double)

    case end(x:
Double, y: Double)

    case center(x:
Double, y: Double)

}




var point = Point.start(x:
0, y: 0)

point = .end(x:
10, y: 10)

point = .center(x:
5, y: 5)




switchpoint {

case .start(let x,
let y):

    print("起点((x),(y))")

case .end(let x,
let y):

    print("终点((x),(y))")

case .center(let x,
let y):

    print("中点((x),(y))")

}


 

人已赞赏
iOS文章

iOS应用首次启动引入导图

2020-2-26 18:25:54

iOS文章

iOS用boundingRectWithSize计算label自适应高度那点事

2020-2-26 20:25:23

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索