Skip to content

Instantly share code, notes, and snippets.

@safecat
Created July 7, 2015 02:51
Show Gist options
  • Save safecat/e377aa20c77237819d26 to your computer and use it in GitHub Desktop.
Save safecat/e377aa20c77237819d26 to your computer and use it in GitHub Desktop.
Swift 枚举类型
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
// Enum 类型的名称和成员值都应当以大写开头
enum status{
case ok
case fail
}
enum status2{
case ok, fail
}
var aaa = status.ok
// 在已经确定类型的情况下,可以简写
aaa = .fail
// 地理位置
enum Location{
case hotel(Double, Double, Int, String) // 经度,纬度,星级,地址
case scenic(Double, Double, Double) // 经度,纬度,营业额
}
var huangshan = Location.scenic(114, 30, 11000.12)
var jinjiang = Location.hotel(114, 31, 4, "大街18号")
// 锦江搬家了,升级了
jinjiang = Location.hotel(114, 30, 5, "大街特1号")
// 打印地理位置
switch jinjiang {
case .hotel(let lat, let lng, var star, let address):
print(lat)
case let .scenic(lat, lng, income):
print(lat)
}
// 原始值
enum Direction : String{
case North = "北";
case South = "南";
}
print(Direction.North.rawValue, appendNewline: false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment