swift版的枚举变量

swift版的枚举变量

swift的枚举类型跟普通的类是极为类似的,使用的时候,请不要以为他是一个常量,以下是测试用源码

//
//  ViewController.swift
//  SwiftEnum
//
//  Created by YouXianMing on 15/10/9.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

import UIKit

enum Planet: Int {
    
    case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}

enum CompassPoint: String {
    
    case North, South, East, West
}

enum Barcode {
    
    case UPCA(Int, Int, Int, Int)
    case QRCode(String)
}

enum MinionIndex: Int {
    
    case DAVE, BOB, JERRY, JORGE, KEVIN, MARK, PHIL, STUART, TIM
    
    static let minionNames = [
        
        DAVE   : "Dave",
        BOB    : "Bob",
        JERRY  : "Jerry",
        JORGE  : "Jorge",
        KEVIN  : "Kevin",
        MARK   : "Mark",
        PHIL   : "Phil",
        STUART : "Stuart",
        TIM    : "Tim"]
    
    func minionName() -> String {
        
        if let minionName = MinionIndex.minionNames[self] {
            
            return minionName
            
        } else {
            
            return "Minion"
        }
    }
    
    func minionImage() -> UIImage? {
        
        return UIImage(named: "Minion(minionName())")
    }
}

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        normalExample()
        
        planetExample()
        
        compassPointExample()
        
        barcodeExample()
        
        minionIndexExample()
    }
    
    func normalExample() {
    
        let vegetable = "red pepper"
        
        switch vegetable {
            
        case "celery":
            print("Add some raisins and make ants on a log.")
            
        case "cucumber", "watercress":
            print("That would make a good tea sandwich.")
            
        case let x where x.hasSuffix("pepper"):
            print("Is it a spicy (x)?")
            
        default:
            print("Everything tastes good in soup.")
        }
    }
    
    func planetExample() {
        
        if let planet : Planet = Planet(rawValue: 1) {
            
            // switch 操作
            switch planet {
                
            case .Mercury:
                print("(planet) (planet.rawValue)")
                
            case .Earth:
                print("(planet) (planet.rawValue)")
                
            case .Neptune:
                print("(planet) (planet.rawValue)")
                
            default:
                print("(planet) (planet.rawValue)")
            }
            
        } else {
            
            // 没有这个枚举值
            print("no value")
        }
    }
    
    func compassPointExample() {
        
        if let compassPoint : CompassPoint = CompassPoint(rawValue: "Kxo") {
            
            // switch 操作
            switch compassPoint {
                
            case .North:
                print("(compassPoint) (compassPoint.rawValue)")
                
            case .West:
                print("(compassPoint) (compassPoint.rawValue)")
                
            default:
                print("(compassPoint) (compassPoint.rawValue)")
            }
            
        } else {
            
            // 没有这个枚举值
            print("no value")
        }
    }
    
    func barcodeExample() {
    
        let barCode = Barcode.UPCA(20, 120, 10, 20)
        
        switch barCode {
            
        case .UPCA(20, 120, 10, 20):
            print("YES")
            
        default:
            print("NO")
        }
    }
    
    func minionIndexExample() {
    
        print(MinionIndex.DAVE.minionImage())
    }
}

rawValue类型的枚举类型

可以带参数,可以带方法

非 rawValue 类型

原文地址:https://www.cnblogs.com/YouXianMing/p/4868541.html