我一直觉得我写的不是技术,而是感情。教程是我一路上的痕迹。专业技能的成功是最可复制的。我希望我的路能让你少走弯路。我希望我能帮助你抹去知识的尘埃。我希望我能帮助你澄清知识的背景。我希望你和我在未来的技术之巅。
枚举源码地址
基本枚举类型 注:与C和Objective-C不同,Swift默认整数值在创建时不分配枚举。Weight例子中,light,Mid和Heavy不等于隐式0,1和2。
enum Weight { case light case mid case heavy }
使用
let weight = Weight.light print("weight: \(weight)")
打印结果 OC只支持枚举值Int一种类型,Swift枚举值支持四种类型 对于Int类型的枚举
enum Age: Int { case young = 0 case middle = 1 case old = 2 }
let age = Age.young print("age: \(age)")
上面的代码有一种缩写方法
enum Age : Int { case young ,middle , old } let age = Age.old
使用枚举的rawValue属性访问其原始值:
let ageRawValue = Age.old.rawValue
对于字符串类型的枚举成员,例如:
enum DogName:String { case taidi = "taidi" case fadou = "fadou" case xiaogou = "xiaogou" }
let name = DogName.taidi print("name: \(name)")
对于字符串的枚举成员,隐藏的原始值是枚举成员变量的名称,但它规定了int类型才行
enum DogNname:Int { case taidi case fadou case xiaogou case guibing }
let dogName = DogNname(rawValue: 3) print("douName: \(dogName!)")
嵌套枚举
enum Dog { enum Food { case noodles case dumpling case meat } enum Toys { case basketBall case footBall } case taidis case fanius } let pet = Dog.taidis print("pet: \(pet)") let food = Dog.Food.dumpling print("food: \(food)") let toys = Dog.Toys.basketBall print("toys: \(toys)")
迭代枚举(die dai)
enum Cat:CaseIterable { case bigCat case middleCat case oldCat }
let numOfDogKind = Cat.allCases.count print("numOfDogKind = \(numOfDogKind)")
for dogKind in Cat.allCases{ print(dogKind) }
关联值 我们可以定义它Swift枚举存储任何给定类型的相关值,每个枚举的值类型可以不同。
enum Pet{ /名字和年龄 case dog(String,Int) //体重 case cat(Float) }
var result = Pet.dog("老七", 3) result = Pet.cat(2.0) print(result)//cat(2.0)
switch result { case .dog(let name, let age): print("dog: name = \(name), age = \(age).") case .cat(let weight): print("cat: weight = \(weight)公斤")//cat: weight = 2.0公斤 }
如果一个枚举成员的所有相关值都被提取为常量或变量,为了简洁,你只能在成员名称前标记一个let或者var:
var result = Pet.dog("老七", 3) result = Pet.cat(2.0) switch result { case let .dog(name,age): print("dog: name = \(name), age = \(age).") case let .cat(weight): print("cat: weight = \(weight)公斤") }
包含枚举: 同样,你也可以在那里structs或classes中内嵌枚举。
struct DaDog { enum DogKind { case jinmao case taiji case fadou } enum Food { case noodles case dumpling case meat } let kind : DogKind let food : Food }
let dog = DaDog(kind: .fadou, food: .dumpling); print(dog.kind)
方法和属性
enum Device { case iPad, iPhone, AppleTV, AppleWatch func introduced() -> String { switch self { case .iPad: return "iPad" case .iPhone: return "iPhone" case .AppleWatch: return "AppleWatch" case .AppleTV: return "AppleTV" } } }
很明显,我们定义了一个设备枚举iPad, iPhone, AppleTV, AppleWatch,还有一种介绍方法。这里的introduced方法,你可以认为枚举是一个类,introduced是成员的方法,Device.iPhone就是一个Device的实例,case是他的属性,好吧,有了这个对象,Device.iPhone可以认为,Device有匿名属性,现在设置这个属性iPhone。好了,introduced里面的switch self,事实上,它是所有这个匿名属性的场景,比如iPad,iPhone等待,然后根据不同的场景返回不同的值。
print(Device.iPhone.introduced())
属性
不允许将存储属性添加到枚举中,但您仍然可以创建计算属性。当然,计算属性的内容是基于枚举值或枚举相关值。
enum Devices { case iPads, iPhones var year: Int { switch self { case .iPhones: return 2007 case .iPads: return 2010 } } }
print(Devices.iPhones.year)
静态方法
enum Device { case iPad, iPhone, AppleTV, AppleWatch func introduced()-> String {
switch self {
case .iPad: return "iPads"
case .iPhone: return "iPhones"
case .AppleWatch: return "AppleWatchs"
case .AppleTV: return "AppleTVs"
}
}
static func fromSlang(term: String) -> Device? {
if term == "iWatch" {
return .AppleWatch
}
return nil
}
}
print(Device.fromSlang(term: "iWatch"))
Swift也允许你在枚举中使用协议(Protocols)和协议扩展(Protocol Extension)。 Swift协议定义一个接口或类型以供其他数据结构来遵循。enum当然也不例外。我们先从Swift标准库中的一个例子开始. CustomStringConvertible是一个以打印为目的的自定义格式化输出的类型。
该协议只有一个要求,即一个只读(getter)类型的字符串(String类型)。我们可以很容易为enum实现这个协议。
protocol CustomStringConvertible {
var description: String { get }
}
enum Trade :CustomStringConvertible{
case Buy(stock:String,amount:Int)
case Sell(stock:String,amount:Int)
var description: String {
switch self {
case .Buy(_, _):
return "Buy"
case .Sell(_, _):
return "Sell"
}
}
}
print(Trade.Buy(stock: "003100", amount: 100).description)
扩展
枚举也可以进行扩展。最明显的用例就是将枚举的case和method分离,这样阅读你的代码能够简单快速地消化掉enum内容,紧接着转移到方法定义:
import Foundation
enum Device {
case iPad, iPhone, AppleTV, AppleWatch
}
extension Device: CustomStringConvertible{
func introduced() -> String {
switch self {
case .iPad: return "iPad"
case .iPhone: return "iPhone"
case .AppleWatch: return "AppleWatch"
case .AppleTV: return "AppleTV"
}
}
var description: String {
switch self {
case .iPad: return "iPad"
case .iPhone: return "iPhone"
case .AppleWatch: return "AppleWatch"
case .AppleTV: return "AppleTV"
}
}
}
使用
print("introduced: \(Device.iPad.introduced())")
print("description: \(Device.iPad.description)")