美文网首页
Swift 枚举

Swift 枚举

作者: BabyNeedCare | 来源:发表于2020-09-10 13:49 被阅读0次

第一种,也是最简单的枚举:

enum directionType: Int {
    
    case left, right, top, bottom
}

    let myDirection = directionType.left
    
    switch myDirection {
    case .left:
        print("---左---")
    default:
        print("---走开---")
    }

这里要说明一下, 枚举有几个值, Switch就必须在case中完全展示, 实在不需要写这么多, 就用default, 否则会报“Switch must be exhaustive(要全面)”的错误

第二种:
case后, 代表的是字符串,例如left代表“左”。

enum directionTranslationType: String {
    case left = "左"
    case right = "右"
    case top = "头"
    case bottom = "底"
}
 let directOption = directionTranslationType.left       
 print(directOption.rawValue)

单纯打印directOption,只会出现“left”, 但我实际需要的是它代表的中文含义, 那么就通过directOption.rawValue去展示
与第二种对应的有相似的枚举

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"
        }
    }
}

print(Device.iPhone.introduced())
Device枚举, 没定义类型, 就没有rawValue, 感觉前一种较为便捷


第三种:
我觉得这种比较有意思, 类似于在枚举中嵌套方法, 可以调用里面的字段做其他操作

enum Trade {
    case buy(purchaseUnitPrice:Float, Quantity:Int)
    case sell(sellUnitPrice:Float, Quantity:Int)
}

 let tradeInfo = Trade.buy(purchaseUnitPrice: 10, Quantity: 2)  
  switch tradeInfo {
       case .buy(let purchaseUnitPrice, let Quantity):
            print("购买单价:\(purchaseUnitPrice),数量:\(Quantity)") 
        case .sell(let sellUnitPrice, let Quantity):
            print("销售单价:\(sellUnitPrice),数量:\(Quantity)")
  }

第四种:
枚举嵌套:
类似广州,韶关, 都有多个区, 枚举嵌套可以更好的展示

enum Area {
    
    enum GuangZhou: String {
        
        case TianHe = "天河"
        case YueXiu = "越秀"
    }
    
    enum ShaoGuan {
        
        case WuJiang
        case ZhenJiang
    }
}

 print(Area.GuangZhou.TianHe.rawValue)//天河

第五种:
枚举中带有属性

enum SupermarketGoods {
    case Cookies, Drinks, Fishes
    var year: Int {
        switch self {
        case .Cookies:
            return 123
        default:
            return 100
        }
    }
}

 print(SupermarketGoods.Cookies.year)//123

第六种:
泛型

enum Rubbish<T> {
    
    case price(T)
    
    func getPrice() -> T {
        
        switch self {
        case .price(let value):
            return value
        }
        
    }
}

        
print(Rubbish<Int>.price(100).getPrice())
        
print(Rubbish<String>.price("100").getPrice())

这时候必须在Rubbish后加<具体类型>


枚举拓展:

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(Device.AppleTV.description)

print(Device.iPhone.introduced())

协议:

protocol enumProtocol {
    
    var description: String { set get }//要同时实现处理getter, setter
}

enum directionTranslationType: String, enumProtocol {
   
    case left = "左"
    case right = "右"
    case top = "头"
    case bottom = "底"
    
    var description: String {
        
        get {
            
            switch self {
            case .left:
                print("现在在\(self.rawValue)边")
            default:
                print("现在在\(self.rawValue)边")
            }
            
            return self.rawValue
            
        }
        
        set {
            
            print("我被赋值了")
        }
        
    }
}

相关文章

网友评论

      本文标题:Swift 枚举

      本文链接:https://www.haomeiwen.com/subject/mkcpektx.html