//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

enum Movement {
case Left
case Right
case Top
case Bottom
} let aMovement = Movement.Left switch aMovement {
case .Left:
print("left")
default:
print("Unknow")
} if case .Left = aMovement {
print("Left")
} if .Left == aMovement {
print("Left")
} enum Season: Int {
case Spring = 0
case Summer = 1
case Autumn = 2
case Winter = 3
} enum House: String {
case ZhangSan = "I am zhangsan"
case LiSi = "I am lisi"
} let zs = House.ZhangSan
print(zs.rawValue) enum Constants: Double {
case π = 3.14159
case e = 2.71828
case φ = 1.61803398874
case λ = 1.30357
} let pai = Constants.π
print(pai.rawValue) enum CompassPoint: String {
case North, South, East, West
} let n = CompassPoint.North
print(n.rawValue) let s = CompassPoint(rawValue: "South"); enum VNodeFlags : UInt32 {
case Delete = 0x00000001
case Write = 0x00000002
case Extended = 0x00000004
case Attrib = 0x00000008
case Link = 0x00000010
case Rename = 0x00000020
case Revoke = 0x00000040
case None = 0x00000080
} enum Character { enum Weapon {
case Bow
case Sword
case Lance
case Dagger
} enum Helmet {
case Wooden
case Iron
case Diamond
} case Thief
case Warrior
case Knight
} let character = Character.Thief
let weapon = Character.Weapon.Bow
let helmet = Character.Helmet.Iron struct Scharacter {
enum CharacterType {
case Thief
case Warrior
case Knight
} enum Weapon {
case Bow
case Sword
case Lance
case Dagger
} let type: CharacterType
let weapon: Weapon
} let sc = Scharacter(type: .Thief, weapon: .Bow)
print(sc.type) enum Trade {
case Buy(stock: String, amount: Int)
case Sell(stock: String, amount: Int)
} let trade = Trade.Buy(stock: "Car", amount: 100)
if case let Trade.Buy(stock, amount) = trade {
print("buy \(amount) of \(stock)")
} enum Trade0 {
case Buy(String, Int)
case Sell(String, Int)
} let trade0 = Trade0.Buy("Car0", 100)
if case let Trade0.Buy(stock, amount) = trade0 {
print("buy \(amount) of \(stock)")
} enum Wearable {
enum Weight: Int {
case Light = 2
} enum Armor: Int {
case Light = 2
} case Helmet(weight: Weight, armor: Armor) func attributes() -> (weight: Int, armor: Int) {
switch self {
case .Helmet(let w, let a):
return (weight: w.rawValue * 2, armor: a.rawValue * 4) }
}
} let test = Wearable.Helmet(weight: .Light, armor: .Light).attributes()
print(test) enum Device {
case iPad, iPhone, AppleTV, AppleWatch
func introduced() -> String {
switch self {
case .AppleTV: return "\(self) was introduced 2006"
case .iPhone: return "\(self) was introduced 2007"
case .iPad: return "\(self) was introduced 2010"
case .AppleWatch: return "\(self) was introduced 2014"
}
}
}
print (Device.iPhone.introduced()) enum Device1 {
case iPad, iPhone
var year: Int {
switch self {
case .iPad:
return 2010
case .iPhone:
return 2007
}
}
} let iPhone = Device1.iPhone
print(iPhone.year) enum Device2 {
case AppleWatch static func fromSlang(term: String) -> Device2? {
if term == "iWatch" {
return .AppleWatch
}
return nil
}
} print(Device2.fromSlang(term: "iWatch") ?? "nil") enum TriStateSwitch {
case Off, Low, High mutating func next() {
switch self {
case .Off:
self = .Low
case .Low:
self = .High
case .High:
self = .Off
}
}
} var state = TriStateSwitch.Low // 必须使用var
state.next()
state.next() protocol CustomStringConvertible {
var description: String { get }
} enum Trade1: CustomStringConvertible {
case Buy, Sell
var description: String {
switch self {
case .Buy:
return "We're buying something"
case .Sell:
return "We're selling something"
}
}
} let t1 = Trade1.Buy.description protocol AccountCompatible {
var remainingFunds: Int { get }
mutating func addFunds(amount: Int) throws
mutating func removeFunds(amount: Int) throws
} enum Account {
case Empty
case Funds(remaining: Int) enum ErrorInfo: Error {
case OverDraft(amount: Int)
} var remainingFunds: Int {
switch self {
case .Empty:
return 0
case .Funds(let remaining):
return remaining
}
}
} extension Account: AccountCompatible {
mutating func addFunds(amount: Int) throws {
var newAmount = amount
if case let .Funds(remaining) = self {
newAmount += remaining
} if newAmount < 0 {
throw Account.ErrorInfo.OverDraft(amount: newAmount)
}else if newAmount == 0 {
self = .Empty
}else {
self = .Funds(remaining: newAmount)
}
} mutating func removeFunds(amount: Int) throws {
try self.addFunds(amount: amount * -1)
}
} var account = Account.Funds(remaining: 20)
try? account.addFunds(amount: 20)
print("add: ", account.remainingFunds)
try? account.removeFunds(amount: 15)
print ("remove 1: ", account.remainingFunds) do {
try account.removeFunds(amount: 55)
}catch (let errorInfo) {
print(errorInfo)
}
print ("remove 2: ", try? account.removeFunds(amount: 55))

来源:Swift 中枚举高级用法及实践

Advanced & Practical Enum usage in Swift

Swift enum(枚举)使用范例的更多相关文章

  1. Swift Enum 枚举

    前言 枚举是一种自定义的数据类型,在 Swift 中枚举类型拥有相当高的自由度.在 Swift 语言中枚举是一级类型,它拥有在其他语言中只有类才拥有的一些特性,比如实例方法,实例构造器等. 枚举声明的 ...

  2. iOS - Swift Enum 枚举

    1.Enum 的创建 1.1 标准定义 枚举的定义 enum CompassPoint { case North case South case East case West } enum Plane ...

  3. swift 的枚举、结构体、类

    一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...

  4. swift学习——枚举

    swift枚举 1. 枚举基本语法 enum Method { case Add case Sub case Mul case Div } 也可以使用一种更简单的写法 enum Method1{ ca ...

  5. Swift 中枚举

    Swift 中枚举高级用法及实践 字数11017 阅读479 评论0 喜欢20 title: "Swift 中枚举高级用法及实践"date: 2015-11-20tags: [AP ...

  6. c# (ENUM)枚举组合类型的谷歌序列化Protobuf

    c# (ENUM)枚举组合类型的谷歌序列化Protobuf,必须在序列化/反序列化时加上下面: RuntimeTypeModel.Default[typeof(Alarm)].EnumPassthru ...

  7. C#将Enum枚举映射到文本字符串

    介绍 当将以前的C代码移植到C#中时,我快发疯了,因为有很多的数组需要将常量映射到字符串.当我在寻找一个C#的方法来完成的时候,我发现了一个自定义属性和映射的方法. 如何使用代码? 对每一个enum枚 ...

  8. MVC3不能正确识别JSON中的Enum枚举值

    一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...

  9. 161208、Java enum 枚举还可以这么用

    在大部分编程语言中,枚举类型都会是一种常用而又必不可少的数据类型,Java中当然也不会例外.然而,Java中的Enum枚举类型却有着许多你意想不到的用法,下面让我们一起来看看. 先来看一段代码示例: ...

随机推荐

  1. JS核心系列:浅谈原型对象和原型链

    在Javascript中,万物皆对象,但对象也有区别,大致可以分为两类,即:普通对象(Object)和函数对象(Function). 一般而言,通过new Function产生的对象是函数对象,其他对 ...

  2. Node-Webkit打包

    1.node-webkit是什么? NW.js is an app runtime based on Chromium and node.js. You can write native apps i ...

  3. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

  4. Mysql存储引擎比较

    Mysql作为一个开源的免费数据库,在平时项目当中会经常使用到,而在项目当中我们的着重点一般在设计使用数据库上而非mysql本身上,所以在提到mysql的存储引擎时,一般都不曾知道,这里经过网上相关文 ...

  5. scp报错 -bash: scp: command not found

    环境:RHEL6.5 使用scp命令报错: [root@oradb23 media]# scp /etc/hosts oradb24:/etc/ -bash: scp: command not fou ...

  6. git和pycharm管理代码

    首先明白三个概念,服务器代码库,本地代码库,和正在coding的项目. coding完毕后,先通过commit提交到本地代码库,然后通过push再提交server的代码库    git步骤 git c ...

  7. Mysql - 存储过程/自定义函数

    在数据库操作中, 尤其是碰到一些复杂一些的系统, 不可避免的, 会用到函数/自定义函数, 或者存储过程. 实际项目中, 自定义函数和存储过程是越少越好, 因为这个东西多了, 也是一个非常难以维护的地方 ...

  8. mysql 行级锁的使用以及死锁的预防

    一.前言 mysql的InnoDB,支持事务和行级锁,可以使用行锁来处理用户提现等业务.使用mysql锁的时候有时候会出现死锁,要做好死锁的预防. 二.MySQL行级锁 行级锁又分共享锁和排他锁. 共 ...

  9. SqlServer简单数据分页

    手边开发的后端项目一直以来都用的.NET MVC框架,访问数据库使用其自带的EF CodeFirst模式,写存储过程的能力都快退化了 闲来无事,自己写了条分页存储过程,网上类似的文章多的是,这里只列了 ...

  10. 驱动01.LED

    1.写出leds_open,leds_write函数2.1告诉内核这几个函数的存在?定义一个结构体file_operations2.2把这个结构体告诉内核?用register_chrdev(major ...