zoukankan      html  css  js  c++  java
  • 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 类型

  • 相关阅读:
    强网杯2019 随便注
    HCTF2018 WarmUp
    GKCTF2020 部分MISC
    各类文件头及其十六进制标识
    自动发送邮件
    单一接口多种数据验证
    Yaml 文件的操作
    log 文件的操作
    xml 文件的操作
    ddt 使用操作
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4868541.html
Copyright © 2011-2022 走看看