enum枚举完全解读

2015-12-11  本文已影响290人  Hollylord

意义

枚举的最大的意义在于,提高代码的可读性。因此强烈推荐给大家。

基础知识

声明

enum Movement{
    case Left
    case Right
    case Top
    case Bottom
}

定义枚举值

这里Swift可以定义多种类型的枚举值,而OC只能为Int型。

// 映射到整型
enum Movement: Int {
    case Left = 0
    case Right = 1
    case Top = 2
    case Bottom = 3
}

// 同样你可以与字符串一一对应
enum House: String {
    case Baratheon = "Ours is the Fury"
    case Greyjoy = "We Do Not Sow"
    case Martell = "Unbowed, Unbent, Unbroken"
    case Stark = "Winter is Coming"
    case Tully = "Family, Duty, Honor"
    case Tyrell = "Growing Strong"
}

// 或者float double都可以(同时注意枚举中的花式unicode)
enum Constants: Double {
    case π = 3.14159
    case e = 2.71828
    case φ = 1.61803398874
    case λ = 1.30357
}

读取枚举值

倘若你想要读取枚举的值,可以通过rawValue属性来实现:

let bestHouse = House.Stark
print(bestHouse.rawValue)
// prints "Winter is coming"

嵌套枚举(Nesting Enums)

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

关联值(Associated Value)

关联值是将额外信息附加到enum case中的一种极好的方式。说白了就是枚举值绑定了参数。把参数写在枚举里。

enum Trade {
    case Buy
    case Sell
}
func trade(tradeType: Trade, stock: String, amount: Int) {}
enum Trade {
    case Buy(stock: String, amount: Int)
    case Sell(stock: String, amount: Int)
}
func trade(type: Trade) {}

方法

enum Wearable {
    enum Weight: Int {
        case Light = 1
    }
    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: w.rawValue * 4)
        }
    }
}
let woodenHelmetProps = Wearable.Helmet(weight: .Light, armor: .Light).attributes()
print (woodenHelmetProps)
// prints "(2, 4)"
enum Device {
    case AppleWatch
    static func fromSlang(term: String) -> Device? {
        if term == "iWatch" {
            return .AppleWatch
        }
        return nil
    }
}
print (Device.fromSlang("iWatch"))
enum TriStateSwitch {
    case Off, Low, High
    mutating func next() {
        switch self {
        case Off:
            self = Low
        case Low:
            self = High
        case High:
            self = Off
        }
    }
}
var ovenLight = TriStateSwitch.Low
ovenLight.next()
// ovenLight 现在等于.On
ovenLight.next()
// ovenLight 现在等于.Off

这里修改的self指的是,调用这个方法的对象。谁调用这个方法,谁就是self

属性

只能是计算属性,存储属性不允许。

enum Device {
    case iPad, iPhone
    var year: Int {
        switch self {
        case iPhone: return 2007
        case iPad: return 2010
        }
    }
}

进阶知识

协议

protocol CustomStringConvertible { 
  var description: String { get }
}
enum Trade: CustomStringConvertible {
    case Buy, Sell
    var description: String {
        switch self {
        case Buy: return "We're buying something"
        case Sell: return "We're selling something"
        }
    }
}

let action = Trade.Buy
print("this action is \(action)")
// prints: this action is We're buying something

协议的方法生命和enum的扩展 略。

上一篇 下一篇

猜你喜欢

热点阅读